简体   繁体   English

如何从意图过滤器获取文件路径(电子邮件附件/下载)

[英]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 .然后,使用ContentResolveropenInputStream()从该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.

相关问题 使用FileProvider附件将杂散文件路径添加到电子邮件中 - Stray file path added to email on intent using FileProvider attachment Intent过滤器从Android上的gmail应用程序下载附件 - Intent filter to download attachment from gmail apps on Android 如何从电子邮件应用程序中打开我的应用程序的附件? (MIME类型,意图过滤器...) - how to open an attachment with my application from within the email app? (mime type, intent filter …) Android:如何使用intent-filter处理PDF电子邮件附件? - Android: How to use intent-filter to process a PDF email attachment? 从Android中的Intent过滤器获取电子邮件地址 - Get Email Address from Intent Filter in Android 从意图过滤器获取电子邮件地址 - get email address from intent filter 从Gmail上的附件获取文件路径 - Get the file path from attachment on Gmail Android 7 nougat,如何从传入意图的uri获取文件路径? - Android 7 nougat, how to get the file path from uri of incoming intent? 如何从意图数据uri获取文件路径 - how to get file path from intent data uri 注册意图过滤器以通过我的应用打开来自Gmail和本机邮件的电子邮件附件 - Registering intent filter to open email attachment from Gmail and native mail through my app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM