I am experiencing odd behaviors. I am trying to bring up the gallery using an Intent. And then onActivityResult() I want to take the Uri and convert it to a Bitmap. The issue I am facing is that selecting an image first causes my phone to freeze for several seconds. And then by the time it returns to onActivityResult() the resultCode value is -1, even though I selected an image. Am I missing something in my steps?
Launch Gallery Intent
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, GALLERY_REQUEST_CODE);
My onActivityResult
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == GALLERY_REQUEST_CODE) {
Uri selectedImageUri = data.getData();
if (selectedImageUri != null) {
try {
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(
selectedImageUri, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
Bitmap mBitmap = BitmapFactory.decodeFile(filePath);
LogUtil.e(TAG, "mBitmap: " + mBitmap);
ByteArrayOutputStream bs = new ByteArrayOutputStream();
mBitmap.compress(Bitmap.CompressFormat.PNG, 50, bs);
Intent intent = new Intent(CameraActivity.this,
SecondActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP
| Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("image", bs.toByteArray());
startActivity(intent);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} else {
return;
}
}
Update: I have permission in manifest. I am receiving the correct Uri too. But after selecting an image I am always receiving resultCode = -1. It seems that I am receiving a failed binder transaction.
Am I missing any permissions maybe? Thank you in advance!