2017-05-06 69 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); 

}