2017-05-06 95 views
0

即时通讯在我的logcat中出现以下错误。FileUriExposed异常错误

: android.os.FileUriExposedException: file:///storage/emulated/0/download/AllCast.apk exposed beyond app through Intent.getData() 

它只发生在一些设备,即沼泽醇厚和牛轧糖。

这是发生错误

intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/download/" + newnamestring)), "application/vnd.android.package-archive"); 
     intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

     startActivity(intent); 

香港专业教育学院试图加入这一行的代码

intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 

在我的应用程序类也试过这种

StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder(); 
    StrictMode.setVmPolicy(builder.build()); 
    builder.detectFileUriExposure(); 

有谁能够告诉我什么即时通讯做错了。感谢

编辑****

玉家伙即时通讯试图让@CommonsWare答案工作,我可以得到文件鳍下载到供应商的文件夹。但是当我试图处理这个意图时,我的应用崩溃了。这是我的新日志猫

05-07 16:25:35.784 8715-8715/com.example.harrops.h20droidapp2 E/AndroidRuntime: FATAL EXCEPTION: main 
                      Process: mypackagename, PID: 8715 
                      java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.io.File.getName()' on a null object reference 
                       at my package name.Casting_Apps$DownloadFileFromURL.onPostExecute(Casting_Apps.java:273) 

这使我的代码段

File newFile = null; 

     MimeTypeMap mime = MimeTypeMap.getSingleton(); 
     String ext = newFile.getName().substring(newFile.getName().lastIndexOf(".") + 1); 
     String type = mime.getMimeTypeFromExtension(ext); 
     try { 
      Intent intent = new Intent(); 
      intent.setAction(Intent.ACTION_VIEW); 
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { 
       intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 
       Uri contentUri = FileProvider.getUriForFile(getBaseContext(), "my package name", newFile); 
       intent.setDataAndType(contentUri, type); 
      } else { 
       intent.setDataAndType(Uri.fromFile(newFile), type); 
      } 
      startActivityForResult(intent, ACTIVITY_VIEW_ATTACHMENT); 
     } catch (ActivityNotFoundException anfe) { 
      Toast.makeText(getBaseContext(), "No activity found to open this attachment.", Toast.LENGTH_LONG).show(); 
     } 
+0

https://stackoverflow.com/q/38200282/115145 – CommonsWare

+0

谢谢@CommonsWare昨天看着这个。我现在要去看看它。很快就会与大家联系 –

回答

0

这是我去寻找答案..感谢@CommonsWare的帮助

File toInstall = new File(appDirectory, appName + ".apk"); 
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { 
Uri apkUri = FileProvider.getUriForFile(activity, BuildConfig.APPLICATION_ID + ".provider", toInstall); 
Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE); 
intent.setData(apkUri); 
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 
activity.startActivity(intent) 
} else { 
Uri apkUri = Uri.fromFile(toInstall); 
Intent intent = new Intent(Intent.ACTION_VIEW); 
intent.setDataAndType(apkUri, "application/vnd.android.package-archive"); 
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
activity.startActivity(intent); 

}