2

How to save an relative layout as an bitmap image....? on run time I am adding images to relative layout.. then how to save it as an Bitmap Image.

I have no Idea on this concept... Please suggest the solutions for this.

Thank You.

Gaurav Mandlik
  • 525
  • 1
  • 9
  • 42
satish
  • 65
  • 1
  • 9

2 Answers2

9

Create an XML file:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:id="@+id/rlid"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"/>

Then in your activity use this code:

View content = findViewById(R.id.rlid);
content.setDrawingCacheEnabled(true);
Bitmap bitmap = content.getDrawingCache();
File file = new File("/sdcard/" + yourimagename + ".png");
try {
    if (!file.exists()) {
        file.createNewFile();
    }
    FileOutputStream ostream = new FileOutputStream(file);
    bitmap.compress(CompressFormat.PNG, 10, ostream);
    ostream.close();
    content.invalidate();
} catch (Exception e) {
    e.printStackTrace();
} finally {
        content.setDrawingCacheEnabled(false);
}
JJ86
  • 5,055
  • 2
  • 35
  • 64
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
0

try this: put one button in relative layout and then do this process the image stored in sdcard

        btn1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            view.setDrawingCacheEnabled(true);
            view.buildDrawingCache();
            bm = view.getDrawingCache();

            String root = Environment.getExternalStorageDirectory()
                    .toString();
            File myDir = new File(root + "/_images");
            myDir.mkdirs();
            Random generator = new Random();
            int n = 10000;
            n = generator.nextInt(n);
            String fname = "Image-" + n + ".jpg";
            file = new File(myDir, fname);

            Log.i(TAG, "" + file);

            if (file.exists())
                file.delete();
            try {
                FileOutputStream out = new FileOutputStream(file);
                bm.compress(Bitmap.CompressFormat.JPEG, 90, out);
                out.flush();
                out.close();

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
SubbaReddy PolamReddy
  • 2,083
  • 2
  • 17
  • 23