繁体   English   中英

如何将视图另存为图库中的图像?

[英]How can i save a view as a image in gallery?

我正在一个项目中,我拍摄了两个ImageView,并且能够通过从图库中选择这些图像来在该Imageview上设置两个不同的图像。 现在这是一个视图,其中屏幕上有两个图像。 我希望此视图将我的画廊另存为单个图像...我可以知道该怎么做吗???

我使用的代码是...

在xml中...

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

    <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>

在活动类中:

    public class LayoutDisplay2 extends Activity{

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

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

    }

}
}

更新1:

我使用了以下代码,但未将布局保存在图库中:

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();
                System.err.print("Path of saved image." + s);

                try {
                    FileOutputStream out = new FileOutputStream(file);
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
                    out.flush();
                    out.close();
                } catch (Exception e) {

                }
            }

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

    }

}

在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>

我用这段代码检查它是尝试还是捕获

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

            });

在logcat中,我得到了..

    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)

使您的LinearLayoutsetDrawingCacheEnabled(true); 并捕获布局并将其转换为Bitmap ,并将其另存为sdcard中的图像。

在您的Button单击中编写以下代码。 试试下面的代码。

public class LayoutDisplay2 extends Activity{

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

        setContentView(R.layout.layout2);
       ll=(LinearLayout)findViewById(R.id.linearlayout);

     //Add button in your layout and write the below code onclick of button.
    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

        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();
        System.err.print("Path of saved image." + s);

        try {
            FileOutputStream out = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
            out.flush();
            out.close();
        } catch (Exception e) {

        }
            }
         });

在清单中:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM