0

I'm trying to save my images from ImageView to sd card and they are saving to sd card but they are not updating to gallery. Here is my code.

public void save(View v) {
    bitmap = BitmapFactory.decodeResource(getResources(), backgrounds.get(currentPosition)) ;
    File filepath = Environment.getExternalStorageDirectory();
    File dir = new File(filepath.getAbsolutePath()
    + "/Folder/");
    dir.mkdirs();
    String Image = System.currentTimeMillis()+".Png";
    File file = new File(dir, Image);
    Toast.makeText(MainActivity.this, "Image Saved to SD Card",
    Toast.LENGTH_SHORT).show();
    try {
    output = new FileOutputStream(file);
    // Compress into png format image from 0% - 100%
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, output);
    output.flush();
    output.close();
   }
    catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
}
Anjali Tripathi
  • 1,477
  • 9
  • 28

2 Answers2

1

You have to first add mime type to the image

it will work.

MediaScannerConnection.scanFile(
    context, 
    new String[]{ pathToFile1, pathToFile2 }, 
    new String[]{ "audio/mp3", "*/*" }, 
    new MediaScannerConnectionClient()
    {
        public void onMediaScannerConnected()
        {
        }
        public void onScanCompleted(String path, Uri uri)
        {
        }
    });
Dilavar Malek
  • 1,157
  • 11
  • 18
1

Once you saved bitmap as PNG file in sdCard. Make sure that Gallery knows that PNG file has been created.

Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri contentUri = Uri.fromFile(file);
mediaScanIntent.setData(contentUri);
context.sendBroadcast(mediaScanIntent);
Daniel Park
  • 268
  • 3
  • 8