簡體   English   中英

如何從意圖過濾器獲取文件路徑(電子郵件附件/下載)

[英]How to get file path from Intent Filter (email attachment/download)

我在清單中定義了以下 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>

我嘗試了這個問題的部分答案,結果文件名是“false”,我也嘗試了這個 如何從我的活動中檢索我使用此意圖過濾器打開的文件的文件路徑?

編輯:在另一個問題中找到了這個,它在從下載打開時有效,但為從電子郵件打開提供了 NullPointerException。 有誰知道這是為什么?

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);
}

如何從我的活動中檢索我使用此意圖過濾器打開的文件的文件路徑?

有可能不是一個“文件路徑”。 您使用getIntent().getData()來獲取數據的Uri 然后,使用ContentResolveropenInputStream()從該Uri獲取數據的InputStream

從電子郵件中,您可能收到的是 EXTRA_TEXT 而不是 STREAM。 所以流出來為空。

電子郵件永遠不會作為文件存在。 您可以將它們保存到文件中。 但后來它們不再是電子郵件。

所以,下次你被問到:“什么時候電子郵件不是電子郵件?”,你會知道答案,提問者會向你的超凡智慧低頭並要求成為你的門徒。 但我離題了...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM