0

I made an app Which uses VOLLEY. It Downloads wallpapers but and after downloading it is saved in Directory But it is not showing in Gallery.

I am currently using Kitkat 4.4.2

This is what i am Doing in Utils

public void saveImageToSDCard(Bitmap bitmap) {

File myDir = new File(
          Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
          pref.getGalleryName());


myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Wallpaper_" + n + ".jpg";
File file = new File(myDir, fname);

if (file.exists()) file.delete();
try {
    FileOutputStream out = new FileOutputStream(file);
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
    out.flush();
    out.close();
    Toast.makeText(
            _context,
            _context.getString(R.string.toast_saved).replace("#",
                    "\"" + pref.getGalleryName() + "\""),
            Toast.LENGTH_SHORT).show();
    Log.d(TAG, "Wallpaper saved to: " + file.getAbsolutePath()



    );



} catch (Exception e) {
    e.printStackTrace();
    Toast.makeText(_context,
            _context.getString(R.string.toast_saved_failed),

            Toast.LENGTH_SHORT).show();


}

}

  • Your question does not have any code that uses `MediaScannerConnection` and `scanFile()`, or anything else that might teach the MediaStore about your file. We cannot help you with code that we cannot see, in part because we do not know if the code exists. – CommonsWare Jan 11 '16 at 21:03
  • After `out.close()`. Personally, I also call `out.getFD().sync()` between `out.flush()` and `out.close()`, to ensure that all bytes are written to disk, before trying to get the `MediaStore` to index the file. – CommonsWare Jan 11 '16 at 21:18
  • "images are saved in Internal storage" -- not according to your code. You are writing the images to [external storage](https://commonsware.com/blog/2014/04/08/storage-situation-external-storage.html), from the standpoint of the Android SDK. – CommonsWare Jan 11 '16 at 21:29
  • Ok i got it. And i did it. it is working now. this is what i should have to enter ContentValues values = new ContentValues(); values.put(MediaStore.Images.Media.DATA, file.getAbsolutePath()); values.put(MediaStore.Images.Media.MIME_TYPE,"image/jpeg"); _context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,values); – dharmesh garod Jan 12 '16 at 11:27

0 Answers0