aliyun / aliyun-oss-react-native

MIT License
148 stars 101 forks source link

在Android10以上,上传文件失败,原因已找到,请修复 #95

Open codinglesstech opened 8 months ago

codinglesstech commented 8 months ago
public static String getFilePathFromURI(Context context, Uri contentUri) {
    //copy file and send new file path
    String fileName = getFileName(contentUri);
    if (!TextUtils.isEmpty(fileName)) {
        //File copyFile = new File( Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + fileName);
        /**
         * BugFix: No permission After Android 10 ( Api 29), It will make a upload error 
         * /storage/emulated/0/image-646b1008-1611-4fd9-9758-8cdc15f1021d3788506624350752213.jpg
         * 需要将上传文件的缓存放到私有缓存中,不要随意使用保护目录
         */
        File copyFile = new File( context.getExternalCacheDir().getAbsolutePath() + File.separator + fileName);
        FileUtils.copy(context, contentUri, copyFile);
        return copyFile.getAbsolutePath();
    }
    return null;
}