Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android save view to jpg or png

I would like to write an android app that basically layers an overlay on image on another image and then I would like to save the picture with the overlay as a jpg or png. Basically this will be the whole view that I would like to save.

Sample code would be very helpful.

EDIT:

I tried out your suggestions and am getting a null pointer at the Starred Line.

 import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream;  import android.app.Activity; import android.graphics.Bitmap; import android.graphics.Bitmap.CompressFormat; import android.os.Bundle; import android.os.Environment; import android.widget.LinearLayout; import android.widget.TextView;      public class EditPhoto extends Activity {         /** Called when the activity is first created. */      LinearLayout ll = null;      TextView tv = null;         @Override         public void onCreate(Bundle savedInstanceState) {             super.onCreate(savedInstanceState);             setContentView(R.layout.main);             tv = (TextView) findViewById(R.id.text);             ll = (LinearLayout) findViewById(R.id.layout);             ll.setDrawingCacheEnabled(true);             Bitmap b = ll.getDrawingCache();             File sdCard = Environment.getExternalStorageDirectory();             File file = new File(sdCard, "image.jpg");             FileOutputStream fos;       try {        fos = new FileOutputStream(file);        *** b.compress(CompressFormat.JPEG, 95,fos);       } catch (FileNotFoundException e) {        // TODO Auto-generated catch block        e.printStackTrace();       }          }     } 
like image 745
shaneburgess Avatar asked Jun 24 '10 05:06

shaneburgess


People also ask

What image format is best for Android?

In android the best format of Image is PNG as it light compare to JPG,JPEG etc.So its easy to draw and take less time to perform the operation while using these images.

How do I save as JPG or PNG?

Go to File > Save as and open the Save as type drop-down menu. You can then select JPEG and PNG, as well as TIFF, GIF, HEIC, and multiple bitmap formats. Save the file to your computer and it will convert.


1 Answers

You can take advantage of a View's drawing cache.

view.setDrawingCacheEnabled(true); Bitmap b = view.getDrawingCache(); b.compress(CompressFormat.JPEG, 95, new FileOutputStream("/some/location/image.jpg")); 

Where view is your View. The 95 is the quality of the JPG compression. And the file output stream is just that.

like image 154
Moncader Avatar answered Sep 17 '22 02:09

Moncader