Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

save image in gallery android

I am using this code to save the image:

URL url = null;
            try {
                url = new URL("image");
            } catch (MalformedURLException e1) {
                e1.printStackTrace();
            }
            Bitmap bmp = null;
            try {
                bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            OutputStream output;
            File filepath = Environment.getExternalStorageDirectory();
            File dir = new File(filepath.getAbsolutePath() + "/folder name/");
            dir.mkdirs();
            File file = new File(dir, image + ".png");
            Toast.makeText(HomeActivity.this, "Image Saved to SD Card", Toast.LENGTH_SHORT).show();
            try {

                output = new FileOutputStream(file);
                bmp.compress(Bitmap.CompressFormat.PNG, 100, output);
                output.flush();
                output.close();

            }

            catch (Exception e) {
                e.printStackTrace();
            }

The issue is when this code runs on lollipop devices, Images are not showing in gallery. I have to install File Manager to check these images.

With this code:

    MediaStore.Images.Media.insertImage(getContentResolver(), bmp, "image";

Images are saved in camera folder.

I want to show images in gallery with a specific folder name in all android devices.

Please help.

like image 711
android Avatar asked Jul 04 '15 05:07

android


2 Answers

public void saveImageToExternal(String imgName, Bitmap bm) throws IOException {
    //Create Path to save Image
    File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES+appFolder); //Creates app specific folder
    path.mkdirs();
    File imageFile = new File(path, imgName+".png"); // Imagename.png
    FileOutputStream out = new FileOutputStream(imageFile);
    try{
        bm.compress(Bitmap.CompressFormat.PNG, 100, out); // Compress Image
        out.flush();
        out.close();

        // Tell the media scanner about the new file so that it is
        // immediately available to the user.
        MediaScannerConnection.scanFile(context,new String[] { imageFile.getAbsolutePath() }, null,new MediaScannerConnection.OnScanCompletedListener() {
            public void onScanCompleted(String path, Uri uri) {
                 Log.i("ExternalStorage", "Scanned " + path + ":");
                 Log.i("ExternalStorage", "-> uri=" + uri);
            }
        });
    } catch(Exception e) {
        throw new IOException();
    }
}

Worked for me. Thanks for your time

like image 191
android Avatar answered Oct 18 '22 10:10

android


Just you are code is missing the MediaScannerConnection class. This class scans for new files and directories in gallery that are created from your app. See a full demo example demonstrating this. http://whats-online.info/science-and-tutorials/135/how-to-save-an-image-to-gallery-in-android-programmatically/

like image 35
Daniel Nyamasyo Avatar answered Oct 18 '22 10:10

Daniel Nyamasyo