0

我有一個Android應用程序,我可以提供是否拍照或從庫中選擇它。問題是,使用相機拍攝照片效果不錯,但是,當我從庫中選擇照片時,應用程序會出現問題。在檢查照片的大小後,我發現所選照片(縮略圖)的大小非常大,這就是爲什麼當我嘗試將照片存儲在我的數據庫中後,應用程序變慢並崩潰。例如,在我的應用程序中使用相機拍攝的照片大小爲129600字節,但是當我第二次嘗試從庫中將同一張照片加載到我的應用程序時,我發現它的大小現在爲8294400(大得多)!這很奇怪!不知道爲什麼我的照片的大小上傳到我的Android應用程序時太大了

我想知道如果我的方式來處理照片選擇的情況下(requestCode == 2的情況下)是否正確,並且如果我的代碼中有錯誤?

這裏是我的全碼:

private void selectImage() { 

    final CharSequence[] options = { "Take Photo", "Choose from Gallery","Cancel" }; 

    AlertDialog.Builder builder = new AlertDialog.Builder(ScrollingActivity.this); 
    builder.setTitle("Add Photo!"); 
    builder.setItems(options, new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int item) { 
      if (options[item].equals("Take Photo")) 
      { 
       Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
       startActivityForResult(intent, 1); 
      } 
      else if (options[item].equals("Choose from Gallery")) 
      { 
       Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
       startActivityForResult(intent, 2); 

      } 
      else if (options[item].equals("Cancel")) { 
       dialog.dismiss(); 
      } 
     } 
    }); 
    builder.show(); 
} 

protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{ 
    super.onActivityResult(requestCode, resultCode, data); 
    if (resultCode == RESULT_OK) { 
     if (requestCode == 1) { 
      thumbnail = (Bitmap) data.getExtras().get("data"); 
      System.out.println("Image Byte Count: " + thumbnail.getByteCount()); // prints 129600 Bytes. 
     } else if (requestCode == 2) { 

      Uri selectedImage = data.getData(); 
      String[] filePath = { MediaStore.Images.Media.DATA }; 
      Cursor c = getContentResolver().query(selectedImage,filePath, null, null, null); 
      c.moveToFirst(); 
      int columnIndex = c.getColumnIndex(filePath[0]); 
      String picturePath = c.getString(columnIndex); 
      c.close(); 
      thumbnail = (BitmapFactory.decodeFile(picturePath)); 
      System.out.println("Image Byte Count: " + thumbnail.getByteCount()); // prints 8294400 bytes!!! 
     } 
     renderImage(); 
    } 
} 

預先感謝您的幫助!

回答

1

上週我遇到了同樣的問題,並在這個論壇上發現有一個選項可以檢查圖像大小而不會將其加載到內存中。看看BitmapFactory.options下面的代碼是從Stackoverflow中截取的。

   Resources res = mContext.getResources(); 
       int allowedwidth= res.getDimensionPixelSize(R.dimen.albumart_image_width); 
       int allowedheight= res.getDimensionPixelSize(R.dimen.albumart_image_height); 
        holder.improfile.setImageBitmap(
          decodeSampledBitmapFromFile(circlepicture, allowedwidth, allowedheight)); 
      } catch (Exception e) { 
       e.printStackTrace(); 

    } 

public static Bitmap decodeSampledBitmapFromFile(String circlepicture,int reqWidth, int reqHeight) { 

    // First decode with inJustDecodeBounds=true to check dimensions 
    final BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inJustDecodeBounds = true; 
    BitmapFactory.decodeFile(circlepicture, options); 

    // Calculate inSampleSize 
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); 

    // Decode bitmap with inSampleSize set 
    options.inJustDecodeBounds = false; 
    return BitmapFactory.decodeFile(circlepicture, options); 
} 
public static int calculateInSampleSize(
     BitmapFactory.Options options, int reqWidth, int reqHeight) { 
    // Raw height and width of image 
    final int height = options.outHeight; 
    final int width = options.outWidth; 
    int inSampleSize = 1; 

    if (height > reqHeight || width > reqWidth) { 

     final int halfHeight = height/2; 
     final int halfWidth = width/2; 

     // Calculate the largest inSampleSize value that is a power of 2 and keeps both 
     // height and width larger than the requested height and width. 
     while ((halfHeight/inSampleSize) >= reqHeight 
       && (halfWidth/inSampleSize) >= reqWidth) { 
      inSampleSize *= 2; 
     } 
    } 

    return inSampleSize; 
} 
+0

你能否給我提供原始Stackoverflow討論的鏈接?謝謝 – Othmane

+0

謝謝你解決了一些情況,當應用程序捕獲圖像拍攝的照片,但仍然給其他圖像的巨大尺寸。我仍在尋找完整的解決方案! – Othmane

+0

Android文檔指出下面:inJustDecodeBounds 在API級別1 布爾inJustDecodeBounds 如果設置爲true,解碼器將返回null(無位),但出來...領域仍將設置,允許呼叫者查詢位圖而不必爲其像素分配內存。 – Theo

相關問題