gpt4 book ai didi

android - 无需显式声明 MIME 类型即可打开任何类型的 Android 文件

转载 作者:搜寻专家 更新时间:2023-11-01 08:49:49 24 4
gpt4 key购买 nike

我正在使用此方法打开启动 Intent 以在 Android 上打开文件(我已经在评论中描述了我的问题):

public static void openFile(Context context, String fileName) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// intent.setType(FileHelper.getMimeType(file)); // this doesn't work for PDF files
// opens Intent but then the chosen application doesn't open the file
intent.setType("application/pdf"); // this works correctly for PDF files
intent.setData(Uri.parse("file://" + getPath(context) + fileName));
context.startActivity(intent);
}

我想要一个可以打开任何类型文件的单一方法,而不必明确指定 MIME 类型,这可能吗?

最佳答案

打开文件需要 Mime 类型,即我们必须设置数据和类型。您可以使用此功能打开任何文件:

public void openFile(Context ctx,String filepath)   
{
Uri ttt = Uri.parse("file://" + filepath);
Intent intent = new Intent(Intent.ACTION_VIEW);
String arr[] = filepath.split("\\.");
MimeTypeMap myMime = MimeTypeMap.getSingleton();
String mimeType = myMime.getMimeTypeFromExtension(arr[arr.length - 1]);
intent.setDataAndType(ttt, mimeType);
ctx.startActivity(intent);
}

但是,没有扩展名的文件会有问题。

关于android - 无需显式声明 MIME 类型即可打开任何类型的 Android 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24612916/

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