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.
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; layers = d; layers = r.getDrawable(R.drawable.star_big_on); LayerDrawable layerDrawable = new LayerDrawable(layers); imageView.setImageDrawable(layerDrawable);
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()); layerDrawable.draw(canvas); //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
Create a new Drawable XML file, let's call it mylayer.xml:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/yourcapturedimage" /> <item android:drawable="@drawable/star_big_on" /> </layer-list>
Now in your
Activity set the image using that