Morti Morti - 1 year ago 72
Android Question

Android adding image to picture taken with camera

I have been working on a camera app and everything is working with taking and saving the picture. But I would like to have the Imageview I show when taking the picture to be saved in the actual picture when it is taken. Is that possible?

Right now I have been trying to have the imageView in the same Layout as where I draw the camera preview.




I thought that maybe if it was drawn on the same view as the camera is then it would also saw that image together with the picture.

Does anyone know if this is possible or do I have to take another approach? If so, links to other solutions on how to do this would be appreciated :)


Edit: I have been following this camera tutorial
If anyone is wondering how I save the picture etc

Edit2: For clarification, I want the star in this example to be shown in the taken picture after you take it. So if I take a picture of a wall, I want the star to be shown on that wall when you look at the picture in the gallery afterwards, just as it is when taking the picture!

Answer Source

What you exactly want is a LayerDrawable -

A Drawable that manages an array of other Drawables. These are drawn in array order, so the element with the largest index will be drawn on top.

You have two choices to use LayerDrawable.You can either define it in a separate drawable xml and then simply set the image in your ImageView, or you can configure a LayerDrawable dynamically in your code.However as you want this to happen dynamically you would require the programmatic approach mentioned below.

Programmatically using code

You need to convert your Bitmap(which you must have gotten after capturing a picture) into a Drawable.For that use BitmapDrawable like this :

Resources r = getResources();
Drawable d = new BitmapDrawable(r,yourBitmap);//converting bitmap to drawable    
Drawable[] layers = new Drawable[2];
layers[0] = d;
layers[1] = r.getDrawable(R.drawable.star_big_on);
LayerDrawable layerDrawable = new LayerDrawable(layers);

Now you have your ImageView having two images(1.your captured image and 2.star_big_on) set on it.


As you want the newly created image(with 2 images added to it) to be stored in the gallery, you would do something like this in order to store your image :

int width = layerDrawable.getIntrinsicWidth();
int height = layerDrawable.getIntrinsicHeight();
Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap); 
layerDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
//and then the following line will store it in the gallery
MediaStore.Images.Media.insertImage(getContentResolver(), bitmap , "yourTitle" , "yourDescription");

Now by doing like above your image will be added to the end of the gallery.If you want it to be stored in the start of gallery, refer this code.

And if you want the original image which was stored in the gallery just after you clicked a photo, to be deleted from the gallery, you can refer this SO question.In this question you will see something like file.getPath(), which is nothing but the path of the required image to be deleted.So if you want to get the path of the captured image, refer this SO question.Hopefully now you have every idea with you to achieve what you exactly want.

Note : In case you want to know how you use LayerDrawable via XML

Using XML

Create a new Drawable XML file, let's call it mylayer.xml:

<layer-list xmlns:android="">
   <item android:drawable="@drawable/yourcapturedimage" />
   <item android:drawable="@drawable/star_big_on" />

Now in your Activity set the image using that Drawable:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download