How to get content from the gmail content provider

2019-05-31 03:05发布

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 get the file path.

Kindly help regarding this.

1条回答
啃猪蹄的小仙女
2楼-- · 2019-05-31 04:02

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();
    }
}
查看更多
登录 后发表回答