2014-12-05 5 views
1

문제가 있습니다. 이미지를 선택하여 다른 활동으로 전달하고 두 번째 활동 화면에 표시하도록 앱을 작성했습니다. 그러나 고해상도 이미지를받은 후 내 앱이 다운되는 것을 발견했습니다. 내 테스트에서 1440x810 이미지 (크기는 166KB)를 보냈습니다.Android : intIntent에서 고해상도 이미지를받은 앱이 setImageBitmap에 실패했습니다.

첫 번째 활동

public class SecMainActivity extends ActionBarActivity { 
private Button returnButton; 
private ImageView receivedImageView; 
Intent intent; 
Thread handleBitmapImage; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_sec_main); 
    findViews(); 
    handleReceivedInfoToUI(); 
    setActionListener(); 
} 

@Override 
protected void onStart(){ 
    super.onStart(); 
    handleBitmapImage.start(); 

} 
private void findViews(){ 
    returnButton = (Button) findViewById(R.id.returnButton); 
    receivedImageView = (ImageView) findViewById(R.id.receivedImageView);  
} 

private void handleReceivedInfoToUI(){ 
    intent = getIntent(); 
    handleBitmapImage = new Thread(new Runnable(){ 
      @Override 
      public void run(){ 
       byte [] receivedImageByteArray = intent.getByteArrayExtra(MainActivity.EXTRA_BITMAP_BYTE_STREAM); 
       final Bitmap bmp = BitmapFactory.decodeByteArray(receivedImageByteArray, 0, receivedImageByteArray.length); 

       runOnUiThread(new Runnable(){ 
        @Override 
        public void run(){ 
         setReceivedImage(bmp); 
        } 
       }); 
      } 
      }); 

} 

private void setActionListener(){ 
    returnButton.setOnClickListener(new OnClickListener(){ 
     @Override 
     public void onClick(View v){ 
      finish(); 
     } 
    }); 
} 

public void setReceivedImage(Bitmap bmp){ 
    receivedImageView.setImageBitmap(bmp); 
} 
} 

내가 압축하고이 문제를 해결하기 위해 내받은 이미지 크기를 조정해야합니까

public class MainActivity extends ActionBarActivity { 
private Button button; 
private Intent intent ; 
public final static String EXTRA_BITMAP_BYTE_STREAM = "EXTRA_BITMAP_BYTE_STREAM"; 
private static final int PICK_IMAGE = 1; 
private String imageFilePath; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    button = (Button) findViewById(R.id.button); 
    setActionListener(); 
    Intent getImgIntent = new Intent(); 
    getImgIntent .setType("image/*"); 
    getImgIntent .setAction(Intent.ACTION_GET_CONTENT); 
    startActivityForResult(Intent.createChooser(getImgIntent , "Select Picture"), PICK_IMAGE); 
} 
@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if(requestCode == PICK_IMAGE && data != null && data.getData() != null) { 
     Uri _uri = data.getData(); 
     //User had pick an image. 
     Cursor cursor = getContentResolver().query(_uri, new String[] { android.provider.MediaStore.Images.ImageColumns.DATA }, null, null, null); 
     cursor.moveToFirst(); 
     //Link to the image 
     imageFilePath = cursor.getString(0); 
     cursor.close(); 
    } 
    super.onActivityResult(requestCode, resultCode, data); 
} 

private void setActionListener(){ 
    button.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Bitmap resImage; 
      ByteArrayOutputStream outImageByteArrayOutputStream = new ByteArrayOutputStream(); 
      byte [] outImageByteArray; 

      intent = new Intent(MainActivity.this,SecMainActivity.class); 
      File file = new File(imageFilePath); 
      resImage = BitmapFactory.decodeFile(imageFilePath); 
      resImage.compress(Bitmap.CompressFormat.JPEG,100,outImageByteArrayOutputStream); 
      outImageByteArray = outImageByteArrayOutputStream.toByteArray(); 

      intent.putExtra(EXTRA_BITMAP_BYTE_STREAM, outImageByteArray); 
      startActivity(intent); 
     } 
    }); 
} 
} 

두 번째 활동 : 아래

내 코드?

+0

"outOfMemory 오류"가 맞습니까? 그렇지 않다면 Logcat을 친절하게 게시하십시오. – JLONG

답변

3

비트 맵 너비와 높이를 전달하면 다음 기능을 사용하십시오.

public Bitmap getResizedBitmap(Bitmap image, int bitmapWidth, 
      int bitmapHeight) { 
     return Bitmap.createScaledBitmap(image, bitmapWidth, bitmapHeight, 
       true); 
    } 

비트 맵 비율을 같게하고 비트 맵 크기를 줄이려면 최대 크기의 비트 맵을 전달하십시오. 이 함수를 사용할 수 있습니다.

public Bitmap getResizedBitmap(Bitmap image, int maxSize) { 
    int width = image.getWidth(); 
    int height = image.getHeight(); 

    float bitmapRatio = (float)width/(float) height; 
    if (bitmapRatio > 0) { 
     width = maxSize; 
     height = (int) (width/bitmapRatio); 
    } else { 
     height = maxSize; 
     width = (int) (height * bitmapRatio); 
    } 
    return Bitmap.createScaledBitmap(image, width, height, true); 
} 
관련 문제