【发布时间】:2017-05-06 19:12:41
【问题描述】:
我在我的 logcat 中收到以下错误。
: android.os.FileUriExposedException: file:///storage/emulated/0/download/AllCast.apk exposed beyond app through Intent.getData()
它只发生在某些设备上,例如 marsh mellow 和 nougat。
这是发生错误的地方
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 回答工作,我可以获取文件以将 fin 下载到提供程序文件夹。但是当它尝试处理意图时,我的应用程序崩溃了。这是我的新日志猫
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();
}
【问题讨论】:
-
感谢@CommonsWare 昨天在看这个。我现在要试一试。很快就会与结果联系,哈哈