[英]How to get file path from Intent Filter (email attachment/download)
I have defined the following Intent filter in my manifest which allows me to open .ips files that are downloaded from the browser and also from email attachments.我在清单中定义了以下 Intent 过滤器,它允许我打开从浏览器和电子邮件附件下载的 .ips 文件。
<intent-filter
android:icon='@drawable/ic_launcher'
android:label="@string/quick_patcher_label"
android:priority='1'>
<action android:name="android.intent.action.VIEW" />
<action android:name="android.intent.action.EDIT" />
<action android:name="android.intent.action.PICK" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:mimeType="*/*" />
<data android:pathPattern="*.ips" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:mimeType="*/*" android:scheme="http" android:host="*" android:pathPattern=".*\\.ips" />
<data android:mimeType="*/*" android:scheme="https" android:host="*" android:pathPattern=".*\\.ips" />
</intent-filter>
I tried part of the answer for this question which resulted in "false" as the file name, and I tried this one too.我尝试了这个问题的部分答案,结果文件名是“false”,我也尝试了这个。 How can I retrieve the file path of the file I've opened with this intent filter from within my activity?
如何从我的活动中检索我使用此意图过滤器打开的文件的文件路径?
EDIT: Found this in another question, it works when opening from downloads but gives a NullPointerException for opening from emails.编辑:在另一个问题中找到了这个,它在从下载打开时有效,但为从电子邮件打开提供了 NullPointerException。 Anyone know why that is?
有谁知道这是为什么?
public String getRealPathFromURI(Uri contentUri) {
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
How can I retrieve the file path of the file I've opened with this intent filter from within my activity?
如何从我的活动中检索我使用此意图过滤器打开的文件的文件路径?
There might not be a "file path".有可能不是一个“文件路径”。 You use
getIntent().getData()
to get the Uri
of the data.您使用
getIntent().getData()
来获取数据的Uri
。 Then, use ContentResolver
and openInputStream()
to get an InputStream
for the data from that Uri
.然后,使用
ContentResolver
和openInputStream()
从该Uri
获取数据的InputStream
。
From emails, you are probably getting EXTRA_TEXT and not a STREAM.从电子邮件中,您可能收到的是 EXTRA_TEXT 而不是 STREAM。 So the STREAM comes out null.
所以流出来为空。
Emails never exist as a file.电子邮件永远不会作为文件存在。 You can save them to a file.
您可以将它们保存到文件中。 But then they are no longer an email.
但后来它们不再是电子邮件。
So, next time you are asked: "When is an email not an email?", you will know the answer and the asker will bow to your superior wisdom and ask to be your disciple.所以,下次你被问到:“什么时候电子邮件不是电子邮件?”,你会知道答案,提问者会向你的超凡智慧低头并要求成为你的门徒。 But I digress...
但我离题了...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.