简体   繁体   English

android:如何从我的应用程序中打开文件

[英]android: how to open a file out of my app

I'm developing an app, which should open a file after decoding. 我正在开发一个应用,该应用应在解码后打开文件。 So, I want to open a file (for example an image) picked in my app by a default application (if there are several apps for that kind of file then one of apps is to choose by user). 因此,我想打开一个默认应用程序在我的应用程序中选择的文件(例如图像)(如果该文件有多个应用程序,则其中一个应用程序由用户选择)。

For different file types you need to use different intents like the below 对于不同的文件类型,您需要使用以下类似的意图

Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,
"Open image"), 1);

or you could use the following code to get the file type from the file and letthe android system figure out which app it can be opened with 或者您可以使用以下代码从文件中获取文件类型,然后让android系统确定可以使用哪个应用打开

File file = new File("EXAMPLE.JPG");


String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(".JPG");
//You need to find the extension from the file using a split command
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), mime);
startActivityForResult(intent, 10);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM