Question

Here I was using 3 Image_Views inside the Frame_Layout,I just want to save My Frame LayOut With Loaded Image Views to Gallery/SD card

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

    <FrameLayout android:id="@+id/frame" 
        android:background="@color/White" 
        android:layout_marginTop="30dp"
        android:layout_marginBottom="150dp"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent">

      <LinearLayout 
            android:orientation="vertical"  
            android:layout_width="fill_parent" android:layout_height="fill_parent"
            android:layout_marginTop="2.0dip" 
            android:layout_marginBottom="2.0dip"
            android:layout_marginLeft="2.0dip"
            android:layout_marginRight="2.0dip"  >



    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"

        android:layout_weight="1.0"
        android:orientation="vertical" >
        <FrameLayout android:layout_height="0.0dip" android:layout_width="fill_parent" 
                android:background="@color/BurlyWood"  android:layout_weight="0.3">
        <ImageView android:id="@+id/photoImage" android:background="@color/BurlyWood" android:clickable="true" 
                        android:layout_width="fill_parent" android:layout_height="fill_parent" 
                           />
        </FrameLayout>
        <FrameLayout android:layout_height="0.0dip" android:layout_width="fill_parent" 
                android:background="@color/BurlyWood"  android:layout_weight="0.3">
        <ImageView android:id="@+id/photoImage1" android:background="@color/BurlyWood" android:clickable="true" 
                        android:layout_width="fill_parent" android:layout_height="fill_parent"    />
        </FrameLayout>
        <FrameLayout android:layout_height="0.0dip" android:layout_width="fill_parent" 
                android:background="@color/BurlyWood" android:layout_weight="0.3">
        <ImageView android:id="@+id/photoImage2" android:background="@color/BurlyWood" android:clickable="true" 
                        android:layout_width="fill_parent" android:layout_height="fill_parent"    />
        </FrameLayout>
         </LinearLayout>
         </LinearLayout>



       <ImageView
           android:id="@+id/frame1"
           android:background="@drawable/ic_border3"
           android:layout_width="fill_parent"
           android:layout_height="fill_parent"
            />



    </FrameLayout>

  <LinearLayout android:gravity="center" android:layout_width="0.0dip" android:layout_height="40.0dip" android:layout_weight="1.0">
                        <Button android:textColor="@color/white" 
                            android:layout_width="fill_parent" android:layout_height="fill_parent" 
                            android:text="Save"  />
   </RelativeLayout>

Here is the code i used in java file,

btn_save.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            frm=(FrameLayout)findViewById(R.id.frame);
            frm.setDrawingCacheEnabled(true);
            frm.buildDrawingCache();
            bitmap = frm.getDrawingCache();
            try {
                File rootFile=new File(Environment.getExternalStorageDirectory()+File.separator+"MYBOARD"+File.separator);
                rootFile.mkdirs();

                FileOutputStream fileOutputStream = new FileOutputStream(rootFile); 
                bitmap.compress(CompressFormat.PNG, 100, fileOutputStream);
                fileOutputStream.flush();
                fileOutputStream.close();

            } catch (Exception e) {
            }
        }
    });
Was it helpful?

Solution 2

i tried Like this, Its working now perfectly

    try {
                        File rootFile=new File(Environment.getExternalStorageDirectory().toString()+"/MYBOARD");
                        rootFile.mkdirs();
                        Random generator = new Random();
                        int n = 10000;
                        n = generator.nextInt(n);
                        String fname = "Image-"+ n +".png";

                        resultingfile=new File(rootFile, fname);

                        if (resultingfile.exists ()) resultingfile.delete (); 
                        try {
                               FileOutputStream Fout = new FileOutputStream(resultingfile);
                               bitmap.compress(CompressFormat.PNG, 100, Fout);
                               Fout.flush();
                               Fout.close();

                        } catch (Exception e) {
                               e.printStackTrace();
                        }
} catch (Exception e) {
                }

OTHER TIPS

You can't save Views to SD card or in any I/O streams as they are not Serializable and Parcelable. but you can save images of a ImageView as

    BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
    Bitmap bitmap = drawable.getBitmap();
    try {
        FileOutputStream fileOutputStream = new FileOutputStream("your path to save file"); 
        bitmap.compress(CompressFormat.PNG, 100, fileOutputStream);
        fileOutputStream.flush();
        fileOutputStream.close();
    } catch (Exception e) {
    }

and if you want to save entire frame layout background as Image in SD card in on click of a button, write below code in OnClickListener of a Button...

    FrameLayout frameLayout = (FrameLayout)findViewById(R.id.frame);
    frameLayout.setDrawingCacheEnabled(true);
    frameLayout.buildDrawingCache();
    Bitmap bitmap = frameLayout.getDrawingCache();
    try {
        FileOutputStream fileOutputStream = new FileOutputStream("your path to save file"); 
        bitmap.compress(CompressFormat.PNG, 100, fileOutputStream);
        fileOutputStream.flush();
        fileOutputStream.close();
    } catch (Exception e) {
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top