问题描述
我工作的一个Android应用程序,它应该能够从一个特定的文件夹中打开选中的文件。
我已经尝试过这一点,但选择之后,应用程序,我想打开它,我得到这个消息:
在尝试了很多的thread 1 和thread 2 ,我用code这些行做到这一点:
意向意图=新的意图();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(到/ mnt / SD卡/ XXX / XXX /图片/ XXX.JPG),图像/ *);
myContext.startActivity(意向);
我如何能算出这个?
试试下面的code。我用这$ C $下打开PDF文件。你可以将其用于其他文件也。
档案文件=新的文件(Environment.getExternalStorageDirectory()
Report.pdf);
URI路径= Uri.fromFile(文件);
意图pdfOpenintent =新的意图(Intent.ACTION_VIEW);
pdfOpenintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
pdfOpenintent.setDataAndType(道路,应用程序/ PDF格式);
尝试 {
startActivity(pdfOpenintent);
}赶上(ActivityNotFoundException E){
}
如果你想打开的文件。您可以更改 setDataAndType(道路,应用程序/ PDF格式)
。如果你想打开不同的文件具有相同的目的,你可以使用 Intent.createChooser(意向,打开方式...);
。欲了解更多信息,看看下面的链接
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:
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?
Try the below code. I am using this code for open PDF. You can use it for other files also.
File file = new File(Environment.getExternalStorageDirectory(),
"Report.pdf");
Uri path = Uri.fromFile(file);
Intent pdfOpenintent = new Intent(Intent.ACTION_VIEW);
pdfOpenintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
pdfOpenintent.setDataAndType(path, "application/pdf");
try {
startActivity(pdfOpenintent);
} catch (ActivityNotFoundException e) {
}
If you want to open files. You can change the setDataAndType(path, "application/pdf")
. If you want to open different files with same intent you can use Intent.createChooser(intent, "Open in...");
. For more information look below link
How to make an intent with multiple actions
这篇关于从我的Android应用程序编程方式打开选定的文件(图片,PDF,...)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!