【问题标题】:How to get content from the gmail content provider如何从 gmail 内容提供商获取内容
【发布时间】:2011-11-14 18:17:48
【问题描述】:

我必须从 gmail 应用程序中检索内容的文件路径。 我得到的内容 uri 类似于:

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

我尝试查询内容,但我只得到两列标题和大小。我要获取文件路径。

请对此提供帮助。

【问题讨论】:

    标签: java android android-intent android-contentprovider


    【解决方案1】:

    我认为您无法直接检索文件路径,因为附件存储在 Google 服务器上。但是,您可以使用 ContentResolver 和 InputStream 访问文件数据,并将附件的数据复制到您的存储中。

    受CarvingCode blog的启发,我使用了这个sn-p:

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

    【讨论】:

      猜你喜欢
      • 2010-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-18
      • 1970-01-01
      • 2018-07-07
      • 2022-08-17
      相关资源
      最近更新 更多