Android 尝试查询与第三方应用程序(如ES File explorer)关联的uri时出现SecurityException

Android 尝试查询与第三方应用程序(如ES File explorer)关联的uri时出现SecurityException,android,android-permissions,android-fileprovider,file-manager,android-securityexception,Android,Android Permissions,Android Fileprovider,File Manager,Android Securityexception,在尝试查询与第三方应用程序(如ES File explorer)关联的uri时,我遇到了SecurityException。但是,在尝试从设备的默认文件管理器查询uri时,一切正常 1.打开多媒体资料以选择视频 private void uploadVideo() { try { Intent intent = new Intent(); intent.setType("video/*");

在尝试查询与第三方应用程序(如ES File explorer)关联的uri时,我遇到了SecurityException。但是,在尝试从设备的默认文件管理器查询uri时,一切正常

1.打开多媒体资料以选择视频

 private void uploadVideo() {
        try {
            Intent intent = new Intent();
            intent.setType("video/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(intent, "Select Video"), REQUEST_TAKE_GALLERY_VIDEO);
        } catch (Exception e) {

        }
    }
  • 从幻灯片菜单中选择ES文件资源管理器

  • 我在下面的线路上撞车-

      Cursor returnCursor = context.getContentResolver().query(uri, null,null, null, null);
    
  • 撞车-

    Fatal Exception: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=100, result=-1, data=Intent { dat=content://com.estrongs.files/storage/emulated/0/video1599727800557.mp4 }} to activity {xyz/xyz}: java.lang.SecurityException: Permission Denial: opening provider com.estrongs.android.pop.app.FileContentProvider from ProcessRecord{454561a 26743:xyz/u0a365} (pid=26743, uid=10365) that is not exported from UID 10170
           at android.app.ActivityThread.deliverResults(ActivityThread.java:4596)
           at android.app.ActivityThread.handleSendResult(ActivityThread.java:4638)
           at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:49)
           at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
           at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1976)
           at android.os.Handler.dispatchMessage(Handler.java:106)
           at android.os.Looper.loop(Looper.java:193)
           at android.app.ActivityThread.main(ActivityThread.java:6912)
           at java.lang.reflect.Method.invoke(Method.java)
           at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    

    试图从我收到的评论中创建解决方案,以便在从Uri获取文件路径时处理安全异常。下面的代码未在不同的设备中测试。请随意发表评论

     catch (SecurityException e) {
                //if file open with third party application
                String finalPath;
                if (uri.toString().contains("/storage/emulated/0")) {
                    finalPath = "/storage/emulated/0" + uri.toString().split("/storage/emulated/0")[1];
                } else {
                    finalPath = uri.getPath();
                    if (!TextUtils.isEmpty(finalPath) && !finalPath.startsWith("/")) {
                        finalPath = "/" + finalPath;
                    }
                }
                return finalPath;
            }
    
    其余的呢

    try {
     // doing all kinds of nasty things like trying to get real path from uri and such
     // which sometimes seem to work for Android below Q but never for Android 10 and 11.
    }
    catch (SecurityException e) {     
        // if ( uri.getAuthority().equals("com.estrongs.files")) 
        //     return uri.getPath(); 
         
        if ( new File(uri.getPath()).exists() )
             return uri.getPath();     
        
        return null; 
        }
    

    感谢您的回答..u r使用两个返回语句..因此如果是从Estrong返回的,我们可以返回uri.getPath();没有检查文件是否存在?如果有任何其他第三方应用程序,我们需要检查!如果uri以文件开头,那么我们不应该使用getpath()?…我认为检查也可以发布到catch块中?我已经测试了您的代码,它似乎已经解决了崩溃问题,并且工作正常。一旦彻底测试,我将接受它。我不使用权限检查,因为它似乎不是必需的,而且您也已经注释掉了!评论不用于扩展讨论;这段对话已经结束。
    try {
     // doing all kinds of nasty things like trying to get real path from uri and such
     // which sometimes seem to work for Android below Q but never for Android 10 and 11.
    }
    catch (SecurityException e) {     
        // if ( uri.getAuthority().equals("com.estrongs.files")) 
        //     return uri.getPath(); 
         
        if ( new File(uri.getPath()).exists() )
             return uri.getPath();     
        
        return null; 
        }