How to get content from the gmail content provider
I have to retrieve the file path of the content from gmail app. I get content uri similar to:
content://gmail-ls/messages/mymailid%40gmail.com/4/attachments/0.1/BEST/false
I tried queried for the contents but I get only two columns the title and the size. I want to 开发者_Go百科get the file path.
Kindly help regarding this.
I think you cannot retrieve the file path directly, as the attachment is stored on Google servers. You can, however, access the file data using ContentResolver and InputStream, and copy the attachment's data to your storage.
Inspired by CarvingCode blog, I use this snippet:
if (intent.getScheme().compareTo("content")==0)
{
try
{
InputStream attachment = getContentResolver().openInputStream(data);
if (attachment == null)
Log.e("onCreate", "cannot access mail attachment");
else
{
FileOutputStream tmp = new FileOutputStream("/mnt/sdcard/attachment.pdf");
byte []buffer = new byte[1024];
while (attachment.read(buffer) > 0)
tmp.write(buffer);
tmp.close();
attachment.close();
}
}
catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
精彩评论