简体   繁体   English

使用Intent在Android中打开文件

[英]Open file in android using intent

After using a intent to cause my app to launch from the email application, how do I open the file that caused the launch? 使用意图使我的应用程序从电子邮件应用程序启动后,如何打开导致启动的文件? Normally I just use 通常我只是用

fis = openFileInput("test.txt");

But since its just launching the application from a intent I don't know the name. 但是由于它只是出于意图启动应用程序,所以我不知道名称。 I am assuming it will have something to do with acquiring the URI. 我假设它将与获取URI有关。 I am launching the app using 我正在使用启动应用程序

<intent-filter>
   <action android:name="android.intent.action.VIEW" />
   <category android:name="android.intent.category.DEFAULT" />
   <data android:mimeType="*/*" />
   <data android:pathPattern=".*\\.xls" />
</intent-filter> 

To get InputStream for the file you could use the code like this in your Activity's onCreate() method: 要获取文件的InputStream,可以在Activity的onCreate()方法中使用如下代码:

Uri dataUri = getIntent().getData();
ContentResolver cr = getContentResolver();
InputStream input = cr.openInputStream(dataUri);
//input.read() or whatever you want...

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

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