尝试查询与ES File Explorer等第三方应用程序相关的uri时出现SecurityException

问题描述

尝试查询与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) {

        }
    }
  1. 从幻灯片菜单中选择ES File Explorer。

  2. 我在下面的行崩溃-

      Cursor returnCursor = context.getContentResolver().query(uri,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)

解决方法

if ( uri.toString.startsWith("file://")
    return uri.toString().replace("file://","");

其余的

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; 
    }
,

尝试从从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;
        }

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...