gpt4 book ai didi

android - 在 Android N 中打开下载的文件

转载 作者:行者123 更新时间:2023-11-30 01:15:41 25 4
gpt4 key购买 nike

在我的应用程序中 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,如建议的那样 here我应该使用 FileProvider并获得如下路径:

Uri contentUri = getUriForFile(getContext(), "com.mydomain.fileprovider", newFile);

然后打开它:

ParcelFileDescriptor.openFile(Uri uri,String mode)

按照建议here它说 Uri: A content URI associated with a file, as returned by getUriForFile().

但是在 IDE 中,如果我执行 ParcelfileDescriptor.open... 则没有方法 openFile() Image

只有 open() 以文件作为参数,没有 uri。那么如何在 Android N 中打开文件呢?

注意:我不想用我的应用程序打开文件。例如,如果我下载了一个 pdf,我想用手机上安装的应用程序打开它。

最佳答案

Since Android N if I use this code I get an FileUriExposedException, as suggested here I should use the FileProvider

那部分是正确的。您从 getUriForFile() 获得的 Uri 然后进入您的 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);

关于android - 在 Android N 中打开下载的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37895188/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com