2011-06-21 28 views
1

在我的應用程序中,我下載了一些圖像,並且想知道是否有任何方法可以獲取我已下載的位圖大小。這是一個簡單的問題,但我似乎無法通過Google找到解決方案。有什麼辦法來獲取位圖的存儲大小?

這裏是下載的代碼:

Bitmap b = BitmapFactory.decodeStream((InputStream) new URL(theImageUrl).getContent()); 

回答

1

首先將位圖轉換爲字節[]那麼你可以得到位圖的大小

嘗試用泰豐下面的代碼

Bitmap bitmap = your bitmap object 
ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream); 
    byte[] imageInByte = stream.toByteArray(); 
long length = imageInByte.length; 
1
File file = new File("infilename"); 

// Get the number of bytes in the file 
long length = file.length(); 
+0

謝謝,但我沒有使用File類。我正在使用Bitmap類。你是否建議將位圖轉換爲文件然後執行此操作? – NotACleverMan

+0

然後在此處下載您的代碼下載代碼 – ingsaurabh

+0

爲什麼你需要知道我是如何下載它的?我只是使用了一個Bitmap類。你知道:「位圖b =新的位圖();」 – NotACleverMan

1

使用b.getByteCount();或者你想在下載之前查詢服務器的大小?

編輯:此方法僅適用於API級別12

+0

這將返回膨脹的大小,而不是存儲大小。 – secureboot

相關問題