想要在Gallery / SD卡上保存FrameLayout它包含图像视图吗?

时间:2013-10-20 06:09:14

标签: android android-layout android-imageview android-ui android-framework

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>

这是我在java文件中使用的代码,

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) {
            }
        }
    });

2 个答案:

答案 0 :(得分:1)

您无法将视图保存到SD卡或任何I / O流中,因为它们不是可序列化和可分配的。但您可以将ImageView的图像保存为

    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) {
    }

如果您想要将整个帧布局背景保存为SD卡中的图像,请单击按钮,在按钮的OnClickListener中写下以下代码...

    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) {
    }

答案 1 :(得分:0)

我试过像这样,它现在完美地工作

    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) {
                }