Android : Android: setImageBitmap fail when my app received a high resolution image from Intent

on Thursday, December 4, 2014


I'm having a problem. I writed a app to select and pass an image to another activity and it will be shown on the screen of second activity. However, I found out my app will crash after sending a high resolution image. I sent a 1440x810 image in my test. Below is my code:


First Activity



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);
}
});
}


}


Second Activity



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);
}


}


Do I must compress and resize my received image to solve this problem? Thanks


0 comments:

Post a Comment