Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - get email attachment name in my application

I'm trying to load an email attachment in my application. I can get the content, but I cannot get the file name.

Here's how my intent filter looks like:

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

Here is what I get:

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

In my activity I get the Uri and use it to get the input stream for the file content:

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

Where can I find the file name in this scenario?

like image 777
Mugur Avatar asked May 17 '11 18:05

Mugur


1 Answers

I had the same problem to solve today and ended up finding the solution in another post : Android get attached filename from gmail app The main idea is that the URI you get can be used both for retrieving the file content and for querying to get more info. I made a quick utility function to retrieve the name :

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

You can use it this way in your activity :

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

That worked for me (retrieving the name of PDF files).

like image 100
Gregory Avatar answered Oct 13 '22 01:10

Gregory