| This method can be used to download an image from the internet using a url in Android. This use Android Download Manager to |
| download the file and added it to the Gallery. Downloaded image will be saved to "Pictures" |
| Folder in your internal storage |
1:- Creat This Method Out Of Class
private void downloadImageNew(String filename, String downloadUrlOfImage){
try{
DownloadManager dm = (DownloadManager) activity.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 + filename + ".jpg");
dm.enqueue(request);
Toast.makeText(activity, "Your image Download in internalstorage > Pictures", Toast.LENGTH_SHORT).show();
}
catch (Exception e){
Toast.makeText(activity, "Image download failed.", Toast.LENGTH_SHORT).show();
}
2. Past This Code In Button Click
Download.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
downloadImageNew("Santa cluse","Your Url Here");
}
});
0 Comments
Post a Comment