I have made a feature for my android app which allow users to browse and import images from the gallery. But the app always crashes when a normal or a larger size image is imported but it does not crash when I import very small images. I want the user to be able to import normal sized images with ease and good speed. Here is my code:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_in_card);
openGallery();
}
private void openGallery () {
Intent intent = new Intent();
// Show only images, no videos or anything else
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
// Always show the chooser (if there are multiple options available)
startActivityForResult(Intent.createChooser(intent, getResources().getString(R.string.str_select_pic_gallery)), PICK_IMAGE_REQUEST);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
Uri uri = data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
setPicture(bitmap);
// Log.d(TAG, String.valueOf(bitmap));
} catch (IOException e) {
e.printStackTrace();
}
}
}
@TargetApi(android.os.Build.VERSION_CODES.JELLY_BEAN)
private void setPicture (Bitmap b) {
BitmapDrawable drawable;
if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN) {
drawable = new BitmapDrawable(getResources(), b);
wholeImage.setBackgroundDrawable(drawable);
} else {
drawable = new BitmapDrawable(getResources(), b);
wholeImage.setBackground(drawable);
}
}
However it does not crash in the emulator but it does on every phone. So I can't find the error code since its not being printed in the log cat.