Android - 在我的应用程序中获取电子邮件附件名称

标签 android android-intent android-contentprovider

我正在尝试在我的应用程序中加载电子邮件附件。可以获取内容,但无法获取文件名。

这是我的 Intent 过滤器的样子:

        <intent-filter>
            <action
                android:name="android.intent.action.VIEW" />
            <category
                android:name="android.intent.category.DEFAULT" />
            <data
                android:mimeType="image/jpeg" />
        </intent-filter>

这是我得到的:

INFO/ActivityManager(97): Starting: Intent { act=android.intent.action.VIEW dat=content://gmail-ls/messages/john.doe%40gmail.com/42/attachments/0.1/SIMPLE/false typ=image/jpeg flg=0x3880001 cmp=com.myapp/.ui.email.EmailDocumentActivityJpeg } from pid 97

在我的 Activity 中,我获取了 Uri 并使用它来获取文件内容的输入流:

InputStream is = context.getContentResolver().openInputStream(uri);

在这种情况下,我在哪里可以找到文件名?

最佳答案

我今天有同样的问题要解决,最终在另一篇文章中找到了解决方案:Android get attached filename from gmail app 主要思想是您获得的 URI 既可用于检索文件内容,也可用于查询以获取更多信息。 我做了一个快速实用函数来检索名称:

public static String getContentName(ContentResolver resolver, Uri uri){
    Cursor cursor = resolver.query(uri, null, null, null, null);
    cursor.moveToFirst();
    int nameIndex = cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME);
    if (nameIndex >= 0) {
        return cursor.getString(nameIndex);
    } else {
        return null;
    }
}

您可以在 Activity 中以这种方式使用它:

Uri uri = getIntent().getData();
String name = getContentName(getContentResolver(), uri);

这对我有用(检索 PDF 文件的名称)。

关于Android - 在我的应用程序中获取电子邮件附件名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6035535/

相关文章:

android - 将 UriMatcher 与 ConentProvider 一起使用时如何解析查询参数

Android:为什么 EditText 使滚动变慢?

android - 如何在 Android 中找出图形矩阵的比例

java - 需要应用程序检测来回摇动,目前仅检测单个摇动 Action

当应用程序在 Play 商店上线时,Android 应用程序受 0 台设备支持

android - 在 ContentProvider 中使用 rawQuery 是一种好习惯吗?

android - 如何从 Firebase 数据库中获取随机对象?

同一包中两个 Activity 之间的Android链接

android - 如何根据两个不同 Activity 中其他微调器的位置更改微调器的位置

android - 使用 ContactsContract.Contacts.Data