How to get content from the gmail content provider

与世无争的帅哥 提交于 2019-12-08 03:59:43

问题


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:


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


来源:https://stackoverflow.com/questions/7365557/how-to-get-content-from-the-gmail-content-provider

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!