2011-03-14 106 views
2

我想在字節我應該怎麼做圖像尺寸..得到字節的圖像大小

//這個代碼轉換我的畫廊在這裏我們可以選擇任何現有圖像

Intent i = new Intent(Intent.ACTION_PICK, 
             android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI); 
         i.setType("image/*"); 
         startActivityForResult(i, 2); 

//片段的這段代碼返回從圖庫中選擇圖片的URI,但我想圖像//字節大小也從這裏

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
       super.onActivityResult(requestCode, resultCode, data); 

       if (requestCode == 2) {// 2 for selectimage 
         // Get the data of the image selected? and upload to server 

         if (resultCode == Activity.RESULT_OK) { 
           ImageURI = data.getData(); 
           Log.d("IMAGE PATH",""+ImageURI);}}} 

感謝..

回答

1
ContentResolver resolver = context.getContentResolver(); 
InputStream is = resolver.openInputStream(ImageURI); 
BitmapFactory.Options bounds = new BitmapFactory.Options(); 
BitmapFactory.decodeStream(is,null,bounds); 
int width = bounds.outWidth; 
int height = bounds.outHeight; 
int size = width * height * bytes_per_pixel 
+0

我們在那裏得到bytes_per_pixel代碼 – Balvant 2011-03-14 07:34:12

+0

它的4字節= 1 int。顏色3個字節,alpha 1個字節 – 2011-03-14 08:05:25

+1

您應該添加bounds.inJustDecodeBounds = true;解碼之前。在你的例子中,你已經在本地堆中丟失了內存,而位圖已經創建並且沒有被回收。 – Maxim 2011-07-30 14:50:28