Note that there are some explanatory texts on larger screens.

plurals
  1. POonActivityResult is called before ACTION_PICK is finished
    primarykey
    data
    text
    <p>I'm having some problems selecting an image from my gallery and using that in my application. I use this intent:</p> <pre><code>Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(i, ACTIVITY_SELECT_PHOTO); </code></pre> <p>and that brings me to the gallery where i can pick an image. But the moment I select one, onActivityResult is called immediatly, 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?</p> <pre><code>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(); } } </code></pre> <p>ResultCode = -1 (so RESULT_OK) but it gives me an error:</p> <p>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</p> <p>The nullPointerException comes from a different RequestCode (see "THIS LINE" above)</p> <p>The app stops, but when i restart it, I see that it has picked the photo like it should do</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload