Android 如何从意图中获取文件名?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2971871/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:07:01  来源:igfitidea点击:

How to get the file name from the intent?

androidandroid-intentintentfilter

提问by Sumithran

This is my manifest file. After using intent filter i download the ics file from the mail attachment. When i open the downloaded file it start my application. I need to get the file name and data of the selected file in my application. What should i do in the manifest and the java file. I am very new to android can any one help me????

这是我的清单文件。使用意图过滤器后,我从邮件附件下载 ics 文件。当我打开下载的文件时,它会启动我的应用程序。我需要在我的应用程序中获取所选文件的文件名和数据。我应该在清单和 java 文件中做什么。我对 android 很陌生,有人可以帮我吗????

<application android:label="@string/app_name" android:icon="@drawable/icsicon">
    <activity android:name=".setMIMEfile" android:label="@string/app_name">
     <intent-filter>
         <action android:name="android.intent.action.VIEW" />
         <category android:name="android.intent.category.DEFAULT" />
         <data android:mimeType="*/*" />
        <data android:pathPattern=".*\.ics" />    
     </intent-filter>
</activity>

回答by Karthick

Intent intent = getIntent();                
String name = intent.getData().getPath();

String name will contain your selected file path, from that path you can read your file

字符串名称将包含您选择的文件路径,您可以从该路径读取您的文件

回答by tjhei

Note that

注意

Intent intent = getIntent();
String name = intent.getData().getEncodedPath();

gives you an encoded string, e.g. a space in the file name is encoded as "%20". If you want to open the file, you have to use

给你一个编码的字符串,例如文件名中的空格被编码为“%20”。如果你想打开文件,你必须使用

intent.getData().getPath()

instead.

反而。

回答by Sujith Manjavana

Intent intent = getIntent();
String name = intent.getData().getLastPathSegment();

回答by Karan

You can get the file name using intent.getData().

您可以使用 intent.getData() 获取文件名。