2

I have an app that right now opens up the camera at the click of a button. Then the user can take the picture with the standard camera app which leads to a Cancel/Save option. If cancel if chosen, the picture can be taken again. If the Save option is chosen the image is saved to the Gallery. I want to add some stuff to the image in this Cancel/Save mode before either are clicked.

I think the best way to do this would be to bring the photo into my app and do my modifications there and save with a button. I have no idea how to do this. I know I have to use the onActivityResult function, but thats about it.

Any advice is appreciated.

StarsSky
  • 6,721
  • 6
  • 38
  • 63
benwiz
  • 2,167
  • 3
  • 22
  • 33

1 Answers1

2

When you launch the IMAGE_CAPTURE intent to let the user take a photo, you should pass as parameter the path where you store the image. First you should save the path of the taken picture, then, when the user come back to your activity, manage the bitmap and combine with other elements.

  camera.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    String storageState = Environment.getExternalStorageState();
                    if (storageState.equals(Environment.MEDIA_MOUNTED)) {
                        long time = System.currentTimeMillis();

                        File root = Environment
                                .getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);

                        File dir = new File(root.getAbsolutePath() + "/Camera");
                        if (dir.exists() == false) {
                            dir.mkdirs();
                        }

                        String path = dir.getAbsolutePath() + File.separatorChar
                                + time + ".jpg";
                        filesaved = new File(path);
                        try {
                            if (filesaved.exists() == false) {
                                filesaved.getParentFile().mkdirs();
                                filesaved.createNewFile();
                            }

                        } catch (IOException e) {
                            Toast.makeText(
                                    context,
                                    "Unable to create external file"
                                            + storageState, Toast.LENGTH_LONG).show();
                            return;
                        }


                        uritopass = Uri.fromFile(filesaved);
                        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                        intent.putExtra(MediaStore.EXTRA_OUTPUT, uritopass);
                        startActivityForResult(intent, TAKE_PICTURE);
                    } else {
                        Toast.makeText(
                                context,
                                "External Storeage (SD Card) is required.\n\nCurrent state: "
                                        + storageState, Toast.LENGTH_LONG).show();

                    }
                }

            });

    ...
@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == TAKE_PICTURE) {
                //user took a photo
                File imageFile = new File(filesaved.toString());
                Bitmap bm = decodeFile(imageFile);
                if (bm != null) {
                    bm = combineImages(bm);
                    img.setImageBitmap(bm);
                }
            }
        }
}

...

decodefile method to load the Bitmap from the original file Here.

combineImages method to combine 2 or more Bitmap Here .

Community
  • 1
  • 1
StarsSky
  • 6,721
  • 6
  • 38
  • 63