onActivityResult is called before ACTION_PICK is finished

1.3k views Asked by At

I'm having some problems selecting an image from my gallery and using that in my application. I use this intent:

Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, ACTIVITY_SELECT_PHOTO);

And that brings me to the gallery where I can pick an image. But the moment I select one, onActivityResult is called immediately, while the ACTION_PICK hasn't finished everything. Well, I guess that's the problem. I've read I should try to change the launchMode to singleTop on the activity in the manifest but this didn't work. Or should I change the launchMode of "ACTION_PICK"-activity? And is this even possible?

final static int ACTIVITY_SELECT_PHOTO = 0;
final static int ACTIVITY_CAMERA_PHOTO = 1;

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { 
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 
    Log.d("onActivityresult", "req = " + requestCode + ", result = " + resultCode);
    switch(requestCode) { 
    case ACTIVITY_SELECT_PHOTO:
        if(resultCode == RESULT_OK){  

            Uri selectedImageUri=imageReturnedIntent.getData();
            String actualPath=getRealPathFromURI(selectedImageUri);
            File file=new File(actualPath);
            Bitmap b=decodeFile(file); //this is new bitmap which you can use for your purpose 

            try {
                ByteArrayOutputStream os = new ByteArrayOutputStream();
                b.compress(Bitmap.CompressFormat.PNG, 55, os);
                compressedByteArray = os.toByteArray();
                iv.setImageBitmap(b);
                currentUser.put("picture", compressedByteArray);
                Toast t = Toast.makeText(this, "Uploading picture...", Toast.LENGTH_LONG);
                t.show();
                currentUser.save();
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    case ACTIVITY_CAMERA_PHOTO:
        if(resultCode == RESULT_OK){

            Bundle extras = imageReturnedIntent.getExtras();

            //THE LINE BELOW THIS ONE GIVES NULLPOINTEREX WHICH IS OBVIOUS BECAUSE IT's ANOTHER //CASE      
            Bitmap bmp = (Bitmap)extras.get("data");
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            bmp.compress(Bitmap.CompressFormat.PNG, 55, os);
            compressedByteArray = os.toByteArray();
            bmp = BitmapFactory.decodeByteArray(compressedByteArray, 0, compressedByteArray.length);
            iv.setImageBitmap(bmp);
            currentUser.put("picture", compressedByteArray);

        }
        try {
            Toast t = Toast.makeText(this, "Uploading picture...", Toast.LENGTH_LONG);
            t.show();
            currentUser.save();
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

ResultCode = -1 (so RESULT_OK) but it gives me an error:

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=Intent { dat=content://media/external/images/media/173 }} to activity {com.tapazz/com.tapazz.ShowProfileActivity}: java.lang.NullPointerException

The nullPointerException comes from a different RequestCode (see "THIS LINE" above)

The app stops, but when I restart it, I see that it has picked the photo like it should do

1

There are 1 answers

1
stuckless On BEST ANSWER

This may or may not be related, but your switch() statement doesn't break after each case. So, if it enters the ACTIVITY_SELECT_PHOTO case, it will also process the ACTIVITY_CAMERA_PHOTO as well

switch() {
   case 1:
      break;

   case 2:
      break;
}

Notice that you always need a break; statement at the end of each case.