0

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!

portfoliobuilder
  • 7,556
  • 14
  • 76
  • 136
  • http://stackoverflow.com/questions/2507898/how-to-pick-an-image-from-gallery-sd-card-for-my-app – Praveena Jan 12 '15 at 05:36
  • @Praveen, that is the code I referenced when programming this. It does not matter because my resultCode is -1 after selecting an image. I cannot get an OK status value – portfoliobuilder Jan 12 '15 at 05:42

1 Answers1

2

to make this code work you need to have, write permission check if you have this in your manifest.

<manifest ...>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    ...
</manifest>
Techfist
  • 4,314
  • 6
  • 22
  • 32
  • This does not fix the conflict. It still takes several seconds to return back into the app after image is selected from gallery. The resultCode = -1, yet, I can see that I am retrieving the correct Uri from the image. Additionally, the intent to start a new Activity does not work either. – portfoliobuilder Jan 12 '15 at 05:38
  • 1
    resultCode -1 implies error in processing request, check and post logcat details. – Techfist Jan 12 '15 at 05:40
  • I did not see the usual errors so I did not think much of that. But I do see something now. It is a "Failed binder transaction" – portfoliobuilder Jan 12 '15 at 05:44