如何将布局保存为图库中的图像?

时间:2014-04-17 08:12:18

标签: android

我想保存一个包含两个ImageView的LinearLayout,并且该ImageView中有两个不同的图像。我想在gallery中保存这个布局。任何人都可以告诉我怎么做这个

我使用的代码是:

xml中的

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
     >

    <LinearLayout android:id="@+id/linear2"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
     >

    <ImageView
        android:id="@+id/imageView21"
        android:layout_width="fill_parent"
    android:layout_height="fill_parent"
        android:layout_weight="1"
        android:src="@drawable/ic_launcher" />

    <ImageView
        android:id="@+id/imageView22"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:src="@drawable/ic_launcher" />

    </LinearLayout>

    <RelativeLayout android:id="@+id/linear2"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="5"
     >

    <Button
        android:id="@+id/savelayout2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="Save" />

    </RelativeLayout>

</LinearLayout>

在活动类......

    public class LayoutDisplay2 extends Activity {

    Button save;
    LinearLayout ll;
    ImageView iv1, iv2;
    private static int RESULT_LOAD_IMAGE1 = 1;
    private static int RESULT_LOAD_IMAGE2 = 2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        setContentView(R.layout.layout2);
        ll = (LinearLayout) findViewById(R.id.linear2);
        save = (Button) findViewById(R.id.savelayout2);
        save.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                ll.setDrawingCacheEnabled(true);
                Bitmap bitmap = ll.getDrawingCache();

                String root = Environment.getExternalStorageDirectory()
                        .toString();
                File newDir = new File(root + "/saved_picture");
                newDir.mkdirs();
                Random gen = new Random();
                int n = 10000;
                n = gen.nextInt(n);
                String fotoname = n + ".jpg";
                File file = new File(newDir, fotoname);
                String s = file.getAbsolutePath();
                Log.i("Path of saved image.", s);
                System.err.print("Path of saved image." + s);

                try {
                    FileOutputStream out = new FileOutputStream(file);
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
                    out.flush();
                    Toast.makeText(getApplicationContext(), "Photo Saved",
                            Toast.LENGTH_SHORT).show();
                    out.close();
                } catch (Exception e) {
                    Toast.makeText(getApplicationContext(), "Photo Saved",
                            Toast.LENGTH_SHORT).show();
                    Log.e("Exception", e.toString());
                }
            }

        });
        iv1 = (ImageView) findViewById(R.id.imageView21);
        iv1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent i = new Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

                startActivityForResult(i, RESULT_LOAD_IMAGE1);
            }
        });
        iv2 = (ImageView) findViewById(R.id.imageView22);
        iv2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub

                Intent in = new Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

                startActivityForResult(in, RESULT_LOAD_IMAGE2);
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == RESULT_LOAD_IMAGE1 && resultCode == RESULT_OK
                && null != data) {
            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };

            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);
            cursor.close();

            try {

                iv1.setImageBitmap(BitmapFactory.decodeFile(picturePath));
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
        if (requestCode == RESULT_LOAD_IMAGE2 && resultCode == RESULT_OK
                && null != data) {
            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };
            Toast.makeText(getApplicationContext(), "in second",
                    Toast.LENGTH_SHORT).show();
            Log.i("Second", "in second");
            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);
            cursor.close();
            try {
                iv2.setImageBitmap(BitmapFactory.decodeFile(picturePath));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

}

我得到的例外是:

04-17 13:19:33.908: I/Path of saved image.(30459): /mnt/sdcard/saved_picture/3400.jpg
04-17 13:19:33.918: E/Exception(30459): java.io.FileNotFoundException: /mnt/sdcard/saved_picture/3400.jpg (No such file or directory)

1 个答案:

答案 0 :(得分:0)

只需在清单文件中添加以下权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

编辑:

Boolean isSDPresent = android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);

if(isSDPresent)
{
  // yes 
}
else
{
 // No
}
相关问题