簡體   English   中英

Gmail文件附件下載失敗,並顯示getContentResolver()

[英]Gmail file attachment download is failing with getContentResolver()

我正在編寫一個將與Gmail關聯的文件下載/復制到我的應用程序可以讀取的SD卡上的過程。 當用戶單擊附件時,我的活動被觸發,我使用以下代碼保存到本地存儲;

InputStream in = getContentResolver().openInputStream( intent.getData() );
String ext = intent.getType().equals("text/xml") ? ".xml" : ".gpkg";
localFile = new File( TILE_DIRECTORY, "tmp/"+intent.getDataString().hashCode()+ext);

// If we haven't already cached the file, go get it..
if (!localFile.exists()) {
    localFile.getParentFile().mkdirs();
    FileIO.streamCopy(in, new BufferedOutputStream(new FileOutputStream(localFile)) );
}

FileIO.streamCopy很簡單;

public static void streamCopy(InputStream in, OutputStream out) throws IOException{
    byte[] b = new byte[BUFFER];
    int read;
    while ((read = in.read(b)) != -1) {
            out.write(b, 0, read);
    }
    out.close();
    in.close();
}

所有這些文件在一個小文件上都可以正常工作,但是帶有6Mb附件的文件只能寫入12Kb。 我什至沒有遇到錯誤,整個過程運行得非常快,而我的文件損壞了。

該過程在其自己的線程中運行,並且是具有大量fileIO的大型應用程序的一部分,因此權限/目錄等沒有問題。

我應該對流執行其他操作嗎?

順便說一下,intent.getData()是

content://gmail-ls/mexxx@gmail.com/messages/6847/attachments/0.1/BEST/false

和intent.getType()是

application/octet-stream

謝謝

這段代碼一切正常

InputStream in = new BufferedInputStream(
getContentResolver().openInputStream(intent.getData()) );

File dir = getExternalCacheDir();
File file = new File(dir, Utils.md5(uri.getPath()));

OutputStream out = new BufferedOutputStream( new FileOutputStream(file) );

streamCopy(in, out);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM