2

I want to save a LinearLayout containing two ImageView and there are two different images in that ImageViews. I want to save this Layout in gallery.Can anyone please tell how may i do this

The code that i used is:

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

in the activity class...

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

    }

}

The exception i am getting is:

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
    means you want to take screenshot and save into gallery? – Sagar Maiyad Apr 17 '14 at 08:13
  • kindly check my whole code, and if you dont mind execute it once. I want to save LinearLayout android:id="@+id/linear2" in gallery. This layout contains two images –  Apr 17 '14 at 08:17
  • i tried and it works.. have you mention write storage permision in manifest file? – Sagar Maiyad Apr 17 '14 at 08:23
  • no what permission i have to use may i know please? –  Apr 17 '14 at 08:24
  • 1
  • @Achilles but what in the case if my phone is not habing sdcard but it is having internal storage of 4gb. Why is it not saving in that case?? in this case it is giving 04-17 14:03:37.910: I/Path of saved image.(18177): /storage/sdcard0/saved_picture/7828.jpg but it is not showing the image in gallery –  Apr 17 '14 at 08:36
  • first check that sdcard is available if yes then store into it otherwise store into internal storage. – Sagar Maiyad Apr 17 '14 at 08:39
  • @Achilles how may i check and than save in phone memory can you please tell? –  Apr 17 '14 at 08:40

1 Answers1

0

just add below permission in your manifest file:

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

EDIT:

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

if(isSDPresent)
{
  // yes 
}
else
{
 // No
}
Sagar Maiyad
  • 12,655
  • 9
  • 63
  • 99
  • in the no case what path i will give –  Apr 17 '14 at 08:46
  • follow this http://stackoverflow.com/questions/13161470/saving-file-in-internal-storage-android – Sagar Maiyad Apr 17 '14 at 08:47
  • Thanks for your help i have one more query it is showing the image in very small size in gallery why is it so can you please tell this and how may i show the image that is saved in gallery on full screen and its taking so much of time to come in gallery? –  Apr 17 '14 at 08:57
  • that i dont know.. try to find solution for that. – Sagar Maiyad Apr 17 '14 at 08:59