Android examples for java.net:URL
download Image From URL
import java.io.File; import android.app.DownloadManager; import android.content.Context; import android.database.Cursor; import android.net.Uri; import android.os.Environment; public class Main { private static String IMAGEPATH = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) .toString() + File.separator; public static void downloadImageFromURL(Context context, String fileName, String url) { final DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE); DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url)); if (checkFileExist(fileName, IMAGEPATH)) { File file = new File(IMAGEPATH + fileName); file.delete();//from w w w.j av a 2 s . c om } request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName); final long downloadId = manager.enqueue(request); try { new Thread(new Runnable() { @Override public void run() { boolean downloading = true; while (downloading) { DownloadManager.Query q = new DownloadManager.Query(); q.setFilterById(downloadId); Cursor cursor = manager.query(q); cursor.moveToFirst(); int bytes_downloaded = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR)); int bytes_total = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES)); /// calculate download progress status final double dl_progress = bytes_total != 0 ? (int) ((bytes_downloaded * 100l) / bytes_total) : 0; if (cursor .getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)) == DownloadManager.STATUS_SUCCESSFUL) { downloading = false; } cursor.close(); } } }); } catch (Exception e) { e.printStackTrace(); } } public static boolean checkFileExist(String fileName, String PATH) { File file = new File(PATH + fileName); if (file.exists()) { return true; } else return false; } }