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?