0

In my app I'd like to have the user either take a picture or choose from their library a picture to edit.

My MainActivity:

public void openCamera(View view)
{
    Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);  
    startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);  
}

public void openLibrary(View view)
{
    Intent photoPickerIntent = new Intent(Intent.ACTION_GET_CONTENT);
    photoPickerIntent.setType("image/*");
    startActivityForResult(photoPickerIntent, LIBRARY_PIC_REQUEST);
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{
    Uri path = data.getData();

    Intent intent = new Intent(this, StickerActivity.class);
    intent.putExtra(EXTRA_MESSAGE, path);       
    startActivity(intent);
}

I pass the URI to my next Activity, which lets the user choose a sticker to put on the selected picture. From there, the URI is passed once again to the EditingActivity.

This is what is in my onCreate() method of EditingActivity:

Bundle retrievedExtras = getIntent().getExtras();
    int stickerType = retrievedExtras.getInt(StickerActivity.SELECTED_MESSAGE);
    Uri picturePath = retrievedExtras.getParcelable(MainActivity.EXTRA_MESSAGE);

    paint = new Paint();
    boolean isPortrait = getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
    int frameBufferWidth = isPortrait ? 800 : 1280;
    int frameBufferHeight = isPortrait ? 1280 : 800;
    Bitmap frameBuffer = Bitmap.createBitmap(frameBufferWidth,
            frameBufferHeight, Config.RGB_565);

    try
    {
        chosenPicture = Media.getBitmap(this.getContentResolver(), picturePath);
    } catch (FileNotFoundException e) 
    {
        e.printStackTrace();
    } catch (IOException e) 
    {
        e.printStackTrace();
    }

    canvas = new Canvas(frameBuffer);
    canvas.drawBitmap(chosenPicture, 0, 0, paint);

Now, I've only got the picture shown by using an ImageView so far, however I want the user to be able to touch where they want a sticker on the picture, and have the edited picture saved over the original picture. Right now I can't get the original picture to show in full screen in my EditingActivity as a Bitmap.

I'm fairly new to Android and I've checked many similar questions here that seem to almost be my solution, but not quite.

My questions in a nutshell:

How can I get a Bitmap from a URI? If I am in fact getting a Bitmap from the above code, why isn't it drawing (do I have to override the onDraw() method to use the canvas)? Also, when I get to the point where I can place stickers on the original picture, how do I save the new image (with the stickers) into the device?

1 Answers1

0

Try following code

Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);

Edit

To get URI, use following code.

Uri path = data.getExtras().get("data"); 
Chintan Rathod
  • 25,864
  • 13
  • 83
  • 93
  • I edited my answer. You are not taking right "URI", if i guess. Just change that code, and let me know. – Chintan Rathod Jun 19 '13 at 06:33
  • Have you tried to print your URI in first and second activity? are those same, difference? or correct? – Chintan Rathod Jun 19 '13 at 06:41
  • I have printed the URI in the second activity; I'll make sure to check that it's correct in both activities in the morning - also, I think I have that same piece of code at the start of the second code block? I could be wrong, but doesn't it function the same way your "Uri path = data.getExtras().get("data"); " piece does? – user2499638 Jun 19 '13 at 06:56
  • Hi Chintan, I made sure that the URI is the same through both activities and it is; it comes out as "content://media/external/images/media/(number)" – user2499638 Jun 19 '13 at 15:17