从我的Andr​​oid应用程序编程方式打开选定的文件(图片,PDF,...)? [英] Open a selected file (image, pdf, ...) programmatically from my Android Application?

查看:125
本文介绍了从我的Andr​​oid应用程序编程方式打开选定的文件(图片,PDF,...)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个Android应用程序,它应该能够从一个特定的文件夹中打开选中的文件。

我已经尝试过这一点,但选择之后,应用程序,我想打开它,我得到这个消息:

  

不可能装载

在尝试了很多的<一href="http://stackoverflow.com/questions/5383797/open-an-image-using-uri-in-androids-default-gallery-image-viwer">thread 1 和<一href="http://stackoverflow.com/questions/2169649/get-pick-an-image-from-androids-built-in-gallery-app-programmatically">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(意向,打开方式...); 。欲了解更多信息,看看下面的链接

如何使多个动作 <意向/ P>

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?

解决方案

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

这篇关于从我的Andr​​oid应用程序编程方式打开选定的文件(图片,PDF,...)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆