Pankaj Zanzane
Pankaj Zanzane

Reputation: 1242

How to read a file attached to an email in my application programatically in android?

I know a way to get the Name of attachment in email shown here: Android - get email attachment name in my application.

But could not read actual file which was attached. How do I read that attached file in application programmatically?

I tried this code in which byte array is always null. What am I doing wrong:

public static String getContent(ContentResolver resolver, Uri uri)
{
  Cursor cursor = resolver.query(uri, new String[] { MediaStore.MediaColumns.DATA }, null, null, null);
  cursor.moveToFirst();
  int nameIndex = cursor.getColumnIndex(MediaStore.MediaColumns.DATA);
  Log.d("column", nameIndex + "");
  if (nameIndex >= 0)  
  {
    byte b[] = cursor.getBlob(nameIndex);
    return b.length + "";
  } 
  else 
  {
    return null;
  }
}

Upvotes: 2

Views: 2060

Answers (1)

ArtOfWarfare
ArtOfWarfare

Reputation: 21496

Try this:

Uri uri = getIntent().getData();
InputStream in = getContentResolver().openInputStream(uri);
InputStreamReader reader = new InputStreamReader(in);

At this point, I hand reader to a class I found in a library online called CSVReader, but you could just use the InputStreamReader directly.

Upvotes: 1

Related Questions