1

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,我想用手机上安装的应用程序打开它。

4

1 回答 1

1

由于Android N,如果我使用此代码,我会得到一个FileUriExposedException,如此处建议我应该使用FileProvider

那部分是正确的。Uri你从那里得到的那个getUriForFile()进入你的ACTION_VIEW Intent. 所以你最终会得到类似的东西:

File myFile = new File(result);
Uri uri = FileProvider.getUriForFile(context, YOUR_AUTHORITY, myFile);
Intent myIntent = new Intent(Intent.ACTION_VIEW, uri);
myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myIntent);
于 2016-06-22T10:11:27.690 回答