2

I want to save Image file in Gallery so that Image can be viewed from the Gallery application.

But what I want is to create a separate dir, as like we have for whatsapp Images etc apps in our gallery application.

So far I have written this code to download an image

public void createDir(){
  File dir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), DIR_NAME);
    Log.d(LOG_TAG, "dir pictr :" + dir.toString());
    if (!dir.exists()) {
        dir.mkdir();
        Log.d(LOG_TAG, "dir not exists and created first time");
    } else {
        Log.d(LOG_TAG, "dir exists");
    }
}

Above code created directory inside gallery dir

Uri imageLink = Uri.parse(downloadUrlOfImage);  // this is download link like www.com/abc.jpg

CreateDir();
DownloadManager.Request request = new DownloadManager.Request(imageLink);

File dir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), DIR_NAME);
String absPath = dir.getAbsoultePath();
request.setDestinationUri(Uri.parse(absPath + "image.jpg"));
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);

DownloadManager dm = (DownloadManager) getContext().getSystemService(Context.DOWNLOAD_SERVICE);
dm.enqueue(request);

But this gives me error as java.lang.IllegalArgumentException: Not a file URI: /storage/sdcard0/Pictures/FreeWee/1458148582.jpg

Basically what I want is to save Image and that image must be shown in Gallery Application under Some directory I named.

If not understood please ask, so that I can improve my question. How do I proceed further ?

sap
  • 319
  • 3
  • 7
  • 21
  • Look at http://stackoverflow.com/questions/20859584/how-to-save-image-in-android-gallery. Despite this question's misleading content, the poster also wanted to save to a folder in gallery. Look at the comments of the accepted answer. – Roy Falk Mar 16 '16 at 18:11
  • @RoyFalk but when I introduce the method shown in link and call it when download of image is done. Gallery App shows `unable to load image`. but the image is downloaded. – sap Mar 17 '16 at 12:09
  • It looks like you have two issues in the question. In the future I'd split them to two posts as recommended by the minimal, complete and verifiable example (http://stackoverflow.com/help/mcve). I assume the image download works correctly. If not, try displaying it to make sure. The second issue is what the link talks about. Try taking the code from the link and checking it with an image from the asset folder. Do that and if the code doesn't work, paste that code snippet and error. Minimal, easy questions attract answers – Roy Falk Mar 17 '16 at 14:13
  • @RoyFalk Yes I will try!! but thanks for your help. I got the answer what I wanted. Thanks for your kind suggestion – sap Mar 17 '16 at 15:11

1 Answers1

8

As @RoyFalk pointed out you got 2 issues in your code.

So you can go with this code snippet

String filename = "filename.jpg";
String downloadUrlOfImage = "YOUR_LINK_THAT_POINTS_IMG_ON_WEBSITE";
    File direct =
            new File(Environment
                    .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
                    .getAbsolutePath() + "/" + DIR_NAME + "/");


    if (!direct.exists()) {
        direct.mkdir();
        Log.d(LOG_TAG, "dir created for first time");
    }

    DownloadManager dm = (DownloadManager) getContext().getSystemService(Context.DOWNLOAD_SERVICE);
    Uri downloadUri = Uri.parse(downloadUrlOfImage);
    DownloadManager.Request request = new DownloadManager.Request(downloadUri);
    request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE)
            .setAllowedOverRoaming(false)
            .setTitle(filename)
            .setMimeType("image/jpeg")
            .setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
            .setDestinationInExternalPublicDir(Environment.DIRECTORY_PICTURES,
                    File.separator + DIR_NAME + File.separator + filename);

    dm.enqueue(request);

And you will see Image inside the gallery application under your DIR_NAME. Hope this will help you.

Suraj Palwe
  • 2,080
  • 3
  • 26
  • 42