0

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).

Tom Leese
  • 19,309
  • 12
  • 45
  • 70
  • Use `adb logcat`, DDMS, or the DDMS perspective in Eclipse to examine LogCat and look at the stack traces associated with your "crashes". When it "just reverts back to the previous activity after choosing the image", your `onActivityResult()` will be called -- consider adding more logging or breakpoints to see what is getting invoked. – CommonsWare Jun 11 '11 at 13:31
  • From the logs, my activity from `sendImage` is started correctly, yet I just don't see it... – Tom Leese Jun 11 '11 at 13:46

1 Answers1

1

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.

Tom Leese
  • 19,309
  • 12
  • 45
  • 70