在Android N
下载文件后在我的应用程序中之前,我打开它,如下所示:
Intent myIntent = new Intent(Intent.ACTION_VIEW);
File myFile = new File(result);
String mime = URLConnection.guessContentTypeFromStream(new FileInputStream(myFile));
myIntent.setDataAndType(Uri.fromFile(myFile), mime);
myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myIntent);
因为Android N
如果我使用此代码,我会得到一个FileUriExposedException
,如此处所建议的,我应该使用FileProvider并获取如下路径:
Uri contentUri = getUriForFile(getContext(), "com.mydomain.fileprovider", newFile);
然后打开它:
ParcelFileDescriptor.openFile(Uri uri,String mode)
正如这里所说的那样Uri: A content URI associated with a file, as returned by getUriForFile().
但是在IDE中如果我这样做ParcelfileDescriptor.open...
没有方法openFile()
仅open()
使用 File 作为参数,没有 uri。那么如何在 Android N 中打开文件呢?
注意:我不想用我的应用程序打开文件。例如,如果我下载一个 pdf,我想用手机上安装的应用程序打开它。