7

This is my code and I want the save image button to simply save the image to the gallery.

package com.nk_apps.hip.hop.lyric.wallpapers;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class Wallpapers extends Activity implements OnClickListener {
}
    ImageView display;

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

        display = (ImageView) findViewById(R.id.IVDisplay);
        ImageView image1 = (ImageView) findViewById(R.id.IVimage1);
        ImageView image2 = (ImageView) findViewById(R.id.IVimage2);
        ImageView image3 = (ImageView) findViewById(R.id.IVimage3);
        ImageView image4 = (ImageView) findViewById(R.id.IVimage4);
        ImageView image5 = (ImageView) findViewById(R.id.IVimage5);
        ImageView image6 = (ImageView) findViewById(R.id.IVimage6);
        ImageView image7 = (ImageView) findViewById(R.id.IVimage7);
        ImageView image8 = (ImageView) findViewById(R.id.IVimage8);
        ImageView image9 = (ImageView) findViewById(R.id.IVimage9);
        Button saveImage = (Button) findViewById(R.id.bSaveImage);

        image1.setOnClickListener(this);
        image2.setOnClickListener(this);
        image3.setOnClickListener(this);
        image4.setOnClickListener(this);
        image5.setOnClickListener(this);
        image6.setOnClickListener(this);
        image7.setOnClickListener(this);
        image8.setOnClickListener(this);
        image9.setOnClickListener(this);
        saveImage.setOnClickListener(this);
    }

    public void onClick(View v) {
        // TODO Auto-generated method stub

        switch (v.getId()) {

        case R.id.IVimage1:
            display.setImageResource(R.drawable.aotl);
            break;
        case R.id.IVimage2:
            display.setImageResource(R.drawable.ball_so_hard);
            break;
        case R.id.IVimage3:
            display.setImageResource(R.drawable.eye);
            break;
        case R.id.IVimage4:
            display.setImageResource(R.drawable.faded);
            break;
        case R.id.IVimage5:
            display.setImageResource(R.drawable.hiii_power);
            break;
        case R.id.IVimage6:
            display.setImageResource(R.drawable.i_invented_swag);
            break;
        case R.id.IVimage7:
            display.setImageResource(R.drawable.lifes_a_bitch);
            break;
        case R.id.IVimage8:
            display.setImageResource(R.drawable.mack_truck);
            break;
        case R.id.IVimage9:
            display.setImageResource(R.drawable.opposite);
            break;
        case R.id.bSaveImage:

            break;
        }

    } }

I want to know what to put under the last case. And if I shouldn't put it there could I be told how exactly?

Thanks for any help, really struggling with taking everything in.

Nat Ritmeyer
  • 5,546
  • 8
  • 42
  • 56
user1619978
  • 85
  • 1
  • 1
  • 4

3 Answers3

14

Saving any image in the Gallery can be done by :

MediaStore.Images.Media.insertImage(getContentResolver(), yourBitmap, yourTitle , yourDescription);
Swayam
  • 16,145
  • 13
  • 62
  • 102
  • Sorry, but I really don't know where to put either of these, as I said before I am a real amateur and still going through tutorials. – user1619978 Aug 27 '12 at 12:09
  • This is the code to save any selected image in the gallery. Use this at places where you want to save it. – Swayam Aug 27 '12 at 15:31
  • "case R.id.bSaveImage: InputStream inps= getResources().openRawResource(R.drawable.aotl); Bitmap bmp = BitmapFactory.decodeStream(inps); MediaStore.Images.Media.insertImage(getContentResolver(), bmp, "Wallpaper1", "Description"); break;" Am I going along the right lines? I presumed I would have to point the image to a directory in the SD Card at one point? – user1619978 Aug 29 '12 at 17:17
  • http://russenreaktor.wordpress.com/2010/02/20/solved-android-save-image-to-media-provider-on-android/ – Swayam Aug 29 '12 at 17:24
  • You are welcome my friend! Feel free to accept and upvote! :D – Swayam Aug 29 '12 at 18:23
  • How do you implement the method getContentResolver ? – Machado Feb 20 '15 at 16:40
  • getContentResolver can be called from any Activity Context. You can read more here : http://developer.android.com/reference/android/content/Context.html#getContentResolver(), http://stackoverflow.com/questions/17567326/how-does-getcontentresolver-work, http://stackoverflow.com/questions/3750903/how-can-getcontentresolver-be-called-in-android – Swayam Feb 21 '15 at 05:19
  • in my case this method save nothing... don't know what a problem – Aleksey Timoshchenko Dec 18 '19 at 14:24
  • @AlekseyTimoshchenko with dynamic permissions, you would need to get storage permission from the user before you can save. Without the permission, saving would fail. Check your logs for errors/exceptions. – Swayam Dec 18 '19 at 18:40
  • Eventually, this answer works for me https://stackoverflow.com/a/8722494/5709159 – Aleksey Timoshchenko Dec 18 '19 at 18:47
0

In an onClick Listener, you can use this to save your image to gallery,

FrameLayout mainLayout = (FrameLayout) findViewById(R.id.frame);
                    Random fCount = new Random();
                    int roll = fCount.nextInt(600) + 1;
                    File file = new File(Environment
                            .getExternalStorageDirectory()
                            + File.separator
                            + "/your_folder_name/" + String.valueOf(roll) + ".png");

                    Bitmap b = Bitmap.createBitmap(mainLayout.getWidth(),
                            mainLayout.getHeight(), Bitmap.Config.ARGB_8888);
                    Canvas c = new Canvas(b);
                    mainLayout.draw(c);
                    FileOutputStream fos = null;
                    try {
                        fos = new FileOutputStream(file);
                        if (fos != null) {
                            b.compress(Bitmap.CompressFormat.PNG, 90, fos);
                            fos.close();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
Numair
  • 1,022
  • 1
  • 20
  • 39
  • Sorry, but I really don't know where to put either of these, as I said before I am a real amateur and still going through tutorials. – user1619978 Aug 27 '12 at 12:08
0

Simply, you need a function that you will pass your images to it. See the entire code how to save image to gallery in android programmatically

private void saveImageToExternalStorage(Bitmap finalBitmap) {
        String root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString();
        File myDir = new File(root + "/saved_images_1");
        myDir.mkdirs();
        Random generator = new Random();
        int n = 10000;
        n = generator.nextInt(n);
        String fname = "Image-" + n + ".jpg";
        File file = new File(myDir, fname);
        if (file.exists())
            file.delete();
        try {
            FileOutputStream out = new FileOutputStream(file);
            finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
            out.flush();
            out.close();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
}

On your onClick event call your function and pass your images as bitmap to the function.

switch (v.getId()) {

        case R.id.IVimage1:
           saveImageToExternalStorage(img1);
       break;

       case R.id.IVimage2:
           saveImageToExternalStorage(img2);
       break;

       case R.id.IVimage3:
           saveImageToExternalStorage(img3);
       break;
}
Daniel Nyamasyo
  • 2,278
  • 1
  • 22
  • 23