How to save image from drawable resource to sd card?

Issue

So, i have a view like on the image below. Plus I will add a Save button. You can also see my XML file. I am using this code to load image from drawable folder by pressing the next and previous buttons:

public void onClick(View v) {
        switch (v.getId()){

        case R.id.bNext:
            a++;
            setImage(a);
            break;
        case R.id.bPrevious:
            if(a>0)
            {
                a--;
                setImage(a);  
                break;
            }
        }
    }
    private void setImage(int a){
        if (a  0)
        {
        slika.setImageResource(R.drawable.l2);
        a  1;
        }
        else if (a  1)
        {
        slika.setImageResource(R.drawable.l3);
        a  2;
        }
        else if (a  2)
        {
        slika.setImageResource(R.drawable.l4);
        a  3;
        }
        else if (a  3)
        {
        slika.setImageResource(R.drawable.l5);
        a  4;
        }
        else if (a  4)
        {
        slika.setImageResource(R.drawable.l6);
        a  5;
        }
.
.
.
.
.
        else if (a  55)
        {
        slika.setImageResource(R.drawable.l57);
        a  56;
        }
        }

I need to save the image that is currently on the screen. I found some answers on stackoverflow, but those are all for specific image with known name. I don’t know what image a user will want to save.

enter image description here

<?xml version"1.0" encoding"utf-8"?>
    <RelativeLayout  xmlns:android"http://schemas.android.com/apk/res/android"
        android:layout_width"match_parent"
        android:layout_height"fill_parent"
        android:gravity"center">

                <ImageView
            android:id"@+id/imageView1"
            android:layout_width"fill_parent"
            android:layout_height"fill_parent"
            android:layout_centerHorizontal"true"/>

        <Button
            android:id"@+id/bMenu"
            android:layout_width"80dp"
            android:layout_height"35dp"
            android:layout_alignParentBottom"true"
            android:layout_marginBottom"10dp"
            android:layout_marginLeft"120dp"
            android:background"@drawable/buttons"
            android:gravity"center"
            android:padding"0dp"
            android:text"Menu"
            android:textColor"#ffffff"
            android:textSize"23dp" />

        <Button
            android:id"@+id/bNext"
            android:layout_width"80dp"
            android:layout_height"35dp"
            android:layout_alignParentBottom"true"
            android:layout_marginBottom"10dp"
            android:layout_marginLeft"220dp"
            android:background"@drawable/buttons"
            android:padding"0dp"
            android:text"Next"
            android:textSize"23dp"
            android:textColor"#ffffff" />

        <Button
            android:id"@+id/bPrevious"
            android:layout_width"80dp"
            android:layout_height"35dp"
            android:layout_alignParentBottom"true"
            android:layout_marginBottom"10dp"
            android:layout_marginLeft"20dp"
            android:background"@drawable/buttons"
            android:gravity"center"
            android:padding"0dp"
            android:text"Previous"
            android:textColor"#ffffff"
            android:textSize"23dp" />

    </RelativeLayout>

Solution

String folder  "/sdcard/Pictures/MyAppFolder";

void save(){  
    Imageview  view  (ImageView)findViewById(R.id.cachesView);
    view.buildDrawingCache(); 
    Bitmap yourBitmap  view.getDrawingCache();  
    final File myDir  new File(folder);
    myDir.mkdirs();
    final Random generator  new Random();
    int n  10000;
    n  generator.nextInt(n);
    final String fname  "StyleMe-" + n + ".png";
    File file  new File(myDir, fname);
    if (file.exists()) {
        FileOutputStream out  new FileOutputStream(file);
        yourBitmap.compress(CompressFormat.JPEG, 100, out);
        out.flush();
        out.close();
        sendBroadcast(
            new Intent(Intent.ACTION_MEDIA_MOUNTED,
            Uri.parse("file://"+ Environment.getExternalStorageDirectory()))
        ); // this will refresh the gallery app.
        Toast.makeText(getApplication(), "Image Saved", Toast.LENGTH_SHORT).show();
    }
}

please see

add this method in your class and on click button put

save();

Answered By – Kosh

Leave a Comment