2012-02-16 24 views
4

我能夠獲得圖像的高度和寬度。但有沒有一種方法來檢索存儲在手機中的圖像的大小(以字節或kb或mb爲單位)?如何獲取Android中圖像的大小?

+2

文件文件=新的文件(Environment.getExternalStorageDirectory(), 「image.file」); long length = file.length(); – 2012-02-16 18:37:22

+0

在此之前,你是如何得到圖像的高度和寬度? – 2014-08-01 09:38:10

+0

http://stackoverflow.com/a/8880528/867591 – Ahmed 2014-08-02 18:44:46

回答

8

謝謝您的解答。這就是我終於解決了它:

Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(), 
      R.drawable.ic_launcher); 


    Bitmap bitmap = bitmapOrg; 
    ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream); 
    byte[] imageInByte = stream.toByteArray(); 
    long lengthbmp = imageInByte.length; 

欣賞你的時間和答案:)

8

假設你在談論位圖(而不是ImageView),有一個方法Bitmap.getByteCount()

+0

它只能使用API​​ 12 – mehmet 2014-05-03 20:52:23

+0

@mehmet是的,但是到了兩年前,當它被髮布時,它就起作用了。當我有機會時我會更新。 – 2014-05-03 21:54:45

+1

使用[android.support.v4.graphics.BitmapCompat#getAllocationByteCount](https://developer.android.com/reference/android/support/v4/graphics/BitmapCompat.html#getAllocationByteCount%28android.graphics.Bitmap%29)爲了兼容性。 – 2015-08-27 12:01:49

6

你只需要創建一個新的File對象,如...

String filepath = Environment.getExternalStorageDirectory() + "/file.png"; 
File file = new File(filepath); 
long length = file.length(); 
+1

這給出了文件的大小,而不是圖像。 – 2015-08-27 12:04:15

1

這將返回的真實大小是比賽中的計算機,當你看到右鍵點擊文件的詳細信息。

File file = new File("/sdcard/my_video.mp4"); 
long length = file.length()/1024; // Size in KB 
4
File file = new File("/sdcard/imageName.jpeg"); 
long length = file.length(); 
length = length/1024; 
System.out.println("File Path : " + file.getPath() + ", File size : " + length +" KB");