Android getUriForFile IllegalArgumentException

后端 未结 3 1688
天涯浪人
天涯浪人 2021-01-16 11:02

My code for sending a file with an Intent doesn\'t work with all file sources and I could not find the solution yet:

My app is registered for opening files, so when

相关标签:
3条回答
  • 2021-01-16 11:54
     /file/sdcard/backups/apps/NextGenEndPoint_1.0.apk. 
    

    No that is only a part of the content sheme you got. File Manager app from ASUS will deliver

     content://com.asus.filemanager.OpenFileProvider/file/sdcard/backups/apps/NextGeEndPoint_1.0.apk. 
    

    You better show your code where you determine the 'path' as now you do it wrong.

    0 讨论(0)
  • 2021-01-16 12:00

    In the referenced File:

    <?xml version="1.0" encoding="utf-8"?>
    <paths xmlns:android="http://schemas.android.com/apk/res/android">
      <external-path name="external" path="/"/>      
    </paths>
    

    use files-path instead of external-path

    or

    use Environment.getExternalStorageDirectory()

    0 讨论(0)
  • 2021-01-16 12:00

    The problem was using getUriForFile for file system paths and also for File Provider paths that actually must be used with a content resolver, so the solution was following:

                Uri uri;
                if (ContentResolver.SCHEME_CONTENT.equals(fileUri.getScheme())) {
                    uri = new Uri.Builder()
                            .path(fileUri.getPath())
                            .authority(fileUri.getAuthority())
                            .scheme(ContentResolver.SCHEME_CONTENT)
                            .build();
                } else {
                    uri = getUriForFile(context, "com.myapp.fileprovider", new File(fileUri.getPath()));
                }
    
    0 讨论(0)
提交回复
热议问题