Question

I am using the startActivityForResult to get a picture from the android gallery, however this does not work on my device, yet it works fine on the emulator.

I start the activity like this:

startActivityForResult(new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI), SELECT_IMAGE);

I read the result like this:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == SELECT_IMAGE)
    {
        if (resultCode == Activity.RESULT_OK)
        {
            Uri selectedImage = data.getData();
            Bitmap bitmap;
            try
            {
                bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImage);
            }
            catch (FileNotFoundException e)
            {
                Toast.makeText(TweetMyPic.this, "Failed to load image: " + e.toString(), Toast.LENGTH_LONG).show();
                return;
            }
            catch (IOException e)
            {
                Toast.makeText(TweetMyPic.this, "Failed to load image: " + e.toString(), Toast.LENGTH_LONG).show();
                return;
            }

            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); 

            sendImage(baos.toByteArray());
        }
    }
}

The sendImage function opens a new activity with the data. This works perfectly fine when I use the emulator, but when I am using my device (which has Cooliris instead of the ordinary Gallery app), it either crashes or just reverts back to the previous activity after choosing the image (this only happens when debugging with eclipse).

Was it helpful?

Solution

Android doesn't like it when I send the bitmap data through an intent. So I've changed to saving the image first and then sending the URI.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top