Skip to content

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

@codinglesstech

Description

@codinglesstech
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;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions