Open a selected file (image, pdf, …) progr

2019-01-14 12:30发布

I'm working on an Android application which should be able to open a selected file from a specific folder.

I already tried this, but after selecting which application I want to open it, I got this message:

Impossible loading

After trying a lot of thread 1 and thread 2, I use these lines of code to do it:

Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("/mnt/sdcard/xxx/xxx/Pictures/xxx.jpg"), "image/*");
myContext.startActivity(intent);

How can I figure this out?

7条回答
2楼-- · 2019-01-14 13:31

You can try this

File file = new File(filePath);
MimeTypeMap map = MimeTypeMap.getSingleton();
String ext = MimeTypeMap.getFileExtensionFromUrl(file.getName());
String type = map.getMimeTypeFromExtension(ext);

if (type == null)
    type = "*/*";

Intent intent = new Intent(Intent.ACTION_VIEW);
Uri data = Uri.fromFile(file);

intent.setDataAndType(data, type);

startActivity(intent);
查看更多
登录 后发表回答