http://blog.naver.com/skcjs84?Redirect=Log&logNo=90089483420

http://stackoverflow.com/questions/3107527/android-save-view-to-jpg-or-png

밑의 예제는 null포인터 오류남..쓰불..도저히 모르겠다..


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(); 
      } 
 
        } 
    } 
link|flag

53% accept rate
Nothing on this? I know it is possible, I have seen it done in other apps. – dweebsonduty Jun 25 at 4:41
Can you give us some code describing how you are doing the editing? – Chris Thompson Aug 19 at 20:36
Its more of an idea right now, but I will just have an imageview overlayed over the photo imageview. Unless there is a better way to do it. – dweebsonduty Aug 19 at 20:38
Are you wanting to save the image with the layers (ala PSD) or just as a flat image (ala png)? – Chris Thompson Aug 19 at 20:59
One flat png or jpg would be fine. – dweebsonduty Aug 19 at 22:31

2 Answers

up vote 1 down vote accepted
+150

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.

link|flag
I tried your code, but where does the root starts for the FileOutputStream? beacause I tried to look in the folders of the emulator and couldn't find the saved image... – Sephy Aug 21 at 12:21
1  
It starts at the phones root. So if you want to load something from the sdcard, use the Environment.getExternalStorageDirectory() to get the root for the sdcard. – Moncader Aug 21 at 15:38
So can a LinerLayout be the view in this case and will that grab anything in the LinearLayout? – dweebsonduty Aug 23 at 21:16
@dweebsonduty, Yup that's right. – Moncader Aug 24 at 1:27
It looks like it works thanks and to you goes almost all of my rep :). Thanks guys!! – dweebsonduty Aug 24 at 1:38
up vote 0 down vote
File sdCard = Environment.getExternalStorageDirectory(); 
File file = new File(sdCard, "image.jpg"); 
FileOutputStream fos = new FileOutputStream(file); 

Use fos reference as a 3rd parameter of b.compress() method in Moncader's answer. The image will be stored as image.jpg in root directory of your sd card.

link|flag

+ Recent posts