'How to save and show image in gallery in Android

I am fetching from the server and downloading it in my phone. After downloading when I see image in my phones gallery then there is no image available but when I see image via file manager and in download history there it shows image.

I want to show image gallery in separate folder name like Digi folder like Instagram does.

Below is my code:

String downloadUrlOfImage = "http://www.example.com/images/example.jpg";

String PATH = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).
                                                      getAbsolutePath() + "/" + "Digi" + "/";

File direct = new File(PATH);

if(!direct.exists()){
      direct.mkdir();
  }

DownloadManager dm = (DownloadManager)context.getSystemService(Context.DOWNLOAD_SERVICE);
Uri downloadUri = Uri.parse(downloadUrlOfImage);
DownloadManager.Request request = new DownloadManager.Request(downloadUri);
                                              
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI)
.setTitle("Downloading " + img)
.setMimeType("image/jpeg")
                                   
.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
                                              .setDestinationInExternalFilesDir(context,PATH,img);

dm.enqueue(request);

What am I doing wrong?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source