android 应用打开另一个应用程序,Android – 如何通过Intent在另一个应用程序中打开文件?...

我正在尝试使用其他应用程序打开文件,即使用Gallery打开.jpg,使用Acrobat打开.pdf等.

我遇到的问题是,当我尝试在应用程序中打开文件时,它只打开所选应用程序而不是在应用程序中打开文件.我试过跟随Android open pdf file via Intent,但我必须遗漏一些东西.

public String get_mime_type(String url) {

String ext = MimeTypeMap.getFileExtensionFromUrl(url);

String mime = null;

if (ext != null) {

mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(ext);

}

return mime;

}

public void open_file(String filename) {

File file = new File(Environment.getExternalStoragePublicDirectory(

Environment.DIRECTORY_DOWNLOADS), filename);

// Get URI and MIME type of file

Uri uri = Uri.fromFile(file).normalizeScheme();

String mime = get_mime_type(uri.toString());

// Open file with user selected app

Intent intent = new Intent();

intent.setAction(Intent.ACTION_VIEW);

intent.setData(uri);

intent.setType(mime);

context.startActivity(Intent.createChooser(intent, "Open file with"));

}

据我所知,它返回正确的URI和MIME类型:

URI: file:///storage/emulated/0/Download/Katamari-ringtone-985279.mp3

MIME: audio/mpeg

解决方法:

在此处发布我的更改,以防它可以帮助其他人.我最终将下载位置更改为内部文件夹并添加了内容提供程序.

public void open_file(String filename) {

File path = new File(getFilesDir(), "dl");

File file = new File(path, filename);

// Get URI and MIME type of file

Uri uri = FileProvider.getUriForFile(this, App.PACKAGE_NAME + ".fileprovider", file);

String mime = getContentResolver().getType(uri);

// Open file with user selected app

Intent intent = new Intent();

intent.setAction(Intent.ACTION_VIEW);

intent.setDataAndType(uri, mime);

intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

startActivity(intent);

}

标签:android,android-intent

来源: https://codeday.me/bug/20190528/1169679.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值