0

I am having an application containing different images using ImageView and ViewPager. I want to save the current image shown through ImageView in SD Storage. But it always saved in Phone Storage successfully. I want to save the image in SD Card and also saved image not showing in Gallery Why? Kindly help me out in this issue:


public void SaveIamge() {
        String root = Environment.getExternalStorageDirectory().toString();
        File myDir = new File(root + "/Cute_Baby_Images");
        myDir.mkdirs();
        Random generator = new Random();
        int n = 10000;
        n = generator.nextInt(n);
        String fname = "Image-"+ n +".jpg";
        File file = new File (myDir, fname);
        int currentImagePos = viewPager.getCurrentItem();
        Drawable drawable = viewPager.getResources().getDrawable(images[currentImagePos]);
        Bitmap finalBitmap = ((BitmapDrawable) drawable).getBitmap();

        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();
        }
        Toast.makeText(this, "Sucessfully Save Image", Toast.LENGTH_LONG).show();

    }
Sushin Pv
  • 1,826
  • 3
  • 22
  • 36

2 Answers2

0

Environment.getExternalDirectory() returns phone storage. To get location of SD card, check Find an external SD card location

To make the images appear in gallery, you should let the media scanner scan the file using MediaScannerConnection.scanFile() Check Image, saved to sdcard, doesn't appear in Android's Gallery app

Nabin Bhandari
  • 15,949
  • 6
  • 45
  • 59
0

Create a function called getDirectory()

private static File getDirectory(String variableName, String... paths) {
    String path = System.getenv(variableName);
    if (!TextUtils.isEmpty(path)) {
        if (path.contains(":")) {
            for (String _path : path.split(":")) {
                File file = new File(_path);
                if (file.exists()) {
                    return file;
                }
            }
        } else {
            File file = new File(path);
            if (file.exists()) {
                return file;
            }
        }
    }
    if (paths != null && paths.length > 0) {
        for (String _path : paths) {
            File file = new File(_path);
            if (file.exists()) {
                return file;
            }
        }
    }

    //If any there is no SECONDARY STORAGE are detected return INTERENAL STORAGE
    return Environment.getExternalStorageDirectory();
}

Change your code to

public void SaveIamge() {

    String root = getDirectory("SECONDARY_STORAGE").getAbsolutePath();
    File myDir = new File(root + "/Cute_Baby_Images");
    myDir.mkdirs();
    Random generator = new Random();
    int n = 10000;
    n = generator.nextInt(n);
    String fname = "Image-"+ n +".jpg";
    File file = new File (myDir, fname);
    int currentImagePos = viewPager.getCurrentItem();
    Drawable drawable = viewPager.getResources().getDrawable(images[currentImagePos]);
    Bitmap finalBitmap = ((BitmapDrawable) drawable).getBitmap();

    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();
    }
    Toast.makeText(this, "Sucessfully Save Image", Toast.LENGTH_LONG).show();
    sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + root)));
}

I have added a broadcast to notify the MediaScanner to re-scan the filesystem for new files. This will fix the problem of images are not showing in the gallery.

Sushin Pv
  • 1,826
  • 3
  • 22
  • 36