3

I have to retrieve the filename of the content from gmail app.

i get content uri some thing similar to

content://gmail-ls/messages/mymailid%40gmail.com/4/attachments/0.1/BEST/false

i see some apps getting the file names from it like this app

Kindly help regarding this.

Thanks in advance.

Jana
  • 2,890
  • 5
  • 35
  • 45

2 Answers2

11

Basically, you need to acquire the file system and get a cursor where the info is stored:

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.widget.Toast;

public class CheckIt extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent theIntent = getIntent();
        String attachmentFileName = "No file name found";
        if (theIntent != null && theIntent.getData() != null) {
            Cursor c = getContentResolver().query(
                theIntent.getData(), null, null, null, null);
            c.moveToFirst();
            final int fileNameColumnId = c.getColumnIndex(
                MediaStore.MediaColumns.DISPLAY_NAME);
            if (fileNameColumnId >= 0)
                attachmentFileName = c.getString(fileNameColumnId);
        }
        Toast.makeText(this, attachmentFileName, Toast.LENGTH_LONG).show();
    }

}

In the cursor returned there is another column - MediaStore.MediaColumns.SIZE. At least that's what I get with GMail on my Desire HD. Just try the above code by operning a mail in GMail and hitting the preview button.

Of course, do not forget to add the following to the activity section in the Manifest.xml (do not drop the android.intent.category.LAUNCHER intent-filter section from the activity otherwise it will not be viewable through the launcher):

<intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:mimeType="*/*" /> 
</intent-filter>
Kamen
  • 3,575
  • 23
  • 35
  • i appreciate your effort but my intents are not null instead Cursor C is null. – Jana May 26 '11 at 05:15
  • Hm, that's strange. The filename is shown on my phone when clicking preview button on an attachment in GMail and selecting CkeckIt. Can you provide the code of the activity that you listen for the intent? – Kamen May 26 '11 at 07:50
  • @Kamen how exactly are you then getting the file data? I do see file name and size but where is the path to this data? – Jona Jun 27 '12 at 15:35
  • 2
    Check here: http://carvingcode.blogspot.com/2009/08/how-to-open-gmail-attachments-with.html – Kamen Jul 06 '12 at 15:41
  • 6
    Two problems with this code: you need to [`close()`](http://developer.android.com/reference/android/database/Cursor.html#close%28%29) the cursor after using it, and you should only request the columns you need by passing `new String[]{MediaStore.MediaColumns.DISPLAY_NAME}` as the second argument (`projection`) to `ContentResolver.query()`. – Adam Rosenfield May 24 '13 at 06:03
0

What you need to do is register for the different mime types. Something like this:

<intent-filter>
<action name="android.intent.action.VIEW">
<category name="android.intent.category.DEFAULT">
<data mimetype="text/xml">
</intent-filter>

Then Android will list your app as an option when trying to open attachements from Gmail. Then it will start your app with an intent that gives you the uri.

CaseyB
  • 24,780
  • 14
  • 77
  • 112
  • I have opened the file's content in my app already,but i am not able to retrieve the filename from gmail. – Jana May 25 '11 at 04:59