6

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.

android
  • 221
  • 1
  • 2
  • 7
  • You need to refresh gallery after adding image in gallery. For refresh gallery : http://stackoverflow.com/questions/4144840/how-can-i-refresh-the-gallery-after-i-inserted-an-image-in-android – Darsh Patel Jul 04 '15 at 05:15
  • @VipulPatel: sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse ("file://" + Environment.getExternalStorageDirectory()))); file is file path? – android Jul 04 '15 at 05:19
  • You need to refer this link for your question : http://stackoverflow.com/questions/3300137/how-can-i-refresh-mediastore-on-android – Darsh Patel Jul 04 '15 at 05:26

4 Answers4

16
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

Täg
  • 431
  • 4
  • 17
android
  • 221
  • 1
  • 2
  • 7
  • After almost a week, finally got a solution. thanks :) – Naila Jan 24 '17 at 07:45
  • In addition, with Marshmallow (api 23), we need `Manifest.permission.WRITE_EXTERNAL_STORAGE` permission to be granted. – Täg Mar 31 '19 at 10:48
1

This Worked for me.

Add MediaScannerConnection to scan the file. Specify the file url and mimeType

MediaScannerConnection.scanFile(context,new String[] { image.getAbsolutePath() }, 
new String[] {"images/*"}, new MediaScannerConnection.OnScanCompletedListener() {
    public void onScanCompleted(String path, Uri uri) {
        Log.i("ScanCompleted", "Scanned " + path + ":");
    }
});
Thamim
  • 328
  • 3
  • 14
0

Just change these lines, it will work -

File filepath = Environment.getExternalStorageDirectory().toString();
        File dir = new File(filepath + "/folder name/");
        dir.mkdirs();
        File file = new File(dir, image + ".png");
Exigente05
  • 2,161
  • 3
  • 22
  • 42
0

If you want to save an Image inside a directory then this Code worked for me!

saveImage(data);

            private void saveImage(Bitmap data) {
                File createFolder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"test");
                if(!createFolder.exists())
                createFolder.mkdir();
                File saveImage = new File(createFolder,"downloadimage.jpg");
                try {
                    OutputStream outputStream = new FileOutputStream(saveImage);
                    data.compress(Bitmap.CompressFormat.JPEG,100,outputStream);
                    outputStream.flush();
                    outputStream.close();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
Shahzad Afridi
  • 2,058
  • 26
  • 24