2012-02-16 17 views
4

イメージの高さと幅を取得できます。しかし、電話機に保存されている画像のサイズ(バイトまたはkbまたはmb)を取得する方法はありますか?Androidで画像のサイズを取得する方法は?

+2

ファイルファイル=新しいファイル(Environment.getExternalStorageDirectory()、 "image.file"); long length = file.length(); –

+0

これまでに、画像の高さと幅はどのようになっていましたか? –

+0

http://stackoverflow.com/a/8880528/867591 – Ahmed

答えて

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()というメソッドがあります。あなただけのような新しいFileオブジェクトを作成する必要が

+0

APIのみで動作します。12 – mehmet

+0

@mehmetはい、2年前の時点で、これが掲載された時点で機能しました。チャンスが来たら更新します。 –

+1

[android.support.v4.graphics.BitmapCompat#getAllocationByteCount](https://developer.android.com/reference/android/support/v4/graphics/BitmapCompat.html#getAllocationByteCount%28android.graphics.Bitmap%29)互換性のために。 –

6

...

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

これは画像のサイズではなく、ファイルのサイズを示します。 –

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"); 
関連する問題