Zaid Waseem
Zaid Waseem

Reputation: 381

Save Image From URL in a Specific Location in Android

I'm creating an app that downloads images off the internet! Images are downloaded without a problem! But the problem is that they get downloaded in an unknown folder,and those downloaded images are not seen in the gallery app! How can I download images in a specific gallery folder that they are visible in the gallery as well? Thanks in advance!

Download Method:

    DownloadManager dm = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
    Uri uri = Uri.parse(url);
    DownloadManager.Request request = new DownloadManager.Request(uri);
    request.setDescription("Downloading Wallpaper").setTitle("Downloading");
    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
    myDownloadReference = dm.enqueue(request);

    Toast.makeText(Wallpaper.this, "Downloading..", Toast.LENGTH_SHORT).show();
}

Upvotes: 2

Views: 7371

Answers (4)

susaine
susaine

Reputation: 141

image url to bitmap

 try {
        URL url = new URL("http://....");
        Bitmap image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
    } catch(IOException e) {
        System.out.println(e);
    } 




 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();
    }
    }

Upvotes: 6

Vijay Makwana
Vijay Makwana

Reputation: 486

you need to convert image uri to file and save it in internal or external storage, Convert uri to file like this

File myFile = new File(uri.getPath());

For saving file in local refer this link saving files in storage android

I hope it helps.

Upvotes: 0

Sonam Gupta
Sonam Gupta

Reputation: 341

Try this after downloading file :

 // refresh gallery
            try {
                MediaScannerConnection.scanFile(getActivity(), new String[]{savedImagePath}, null, new MediaScannerConnection.OnScanCompletedListener() {
                    @Override
                    public void onScanCompleted(String path, Uri uri) {
             //   ApplicationUtil.showToast(getActivity(), "onScanCompleted!");
                   }
               });
            } catch (Exception e) {
            }

This will refresh your gallery.

Upvotes: 0

Related Questions