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();
}
https://stackoverflow.com/q/38200282/115145 – CommonsWare
谢谢@CommonsWare昨天看着这个。我现在要去看看它。很快就会与大家联系 –