Skip to content

Instantly share code, notes, and snippets.

@thinzaroo
Created June 17, 2014 07:11
Show Gist options
  • Save thinzaroo/d76696ecd93ea25d7aa2 to your computer and use it in GitHub Desktop.
Save thinzaroo/d76696ecd93ea25d7aa2 to your computer and use it in GitHub Desktop.
This code will download image from URL and save to persistent data folder/SD card using AsyncTask - downloadImagesToSdCard will download image from a web server - onPostExecute will set Image to ImageView, and set as background image of a LinearLayout
private class ImageDownloadAndSave extends AsyncTask<String, Void, Bitmap>
{
String img_URL = "http://upload.wikimedia.org/wikipedia/commons/thumb/5/51/Google.png/320px-Google.png";
String sdCardPath;
private ProgressDialog dialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog = new ProgressDialog(MainActivity.this);
dialog.setMessage("downloading...");
dialog.show();
}
@Override
protected Bitmap doInBackground(String... arg0)
{
downloadImagesToSdCard("","");
return null;
}
private void downloadImagesToSdCard(String downloadUrl,String imageName)
{
try
{
URL url = new URL(img_URL);
/* making a directory in sdcard */
//sdCardPath=Environment.getExternalStorageDirectory().toString();
sdCardPath = "/data/data/" + getPackageName(); //path that stored application's persistent data
File myDir = new File(sdCardPath,"test.jpg");
/* if specified not exist create new */
if(!myDir.exists()){
myDir.mkdir();
}
/* checks the file and if it already exist delete */
String fname = imageName;
File file = new File (myDir, fname);
if (file.exists ())
file.delete ();
/* Open a connection */
URLConnection ucon = url.openConnection();
InputStream inputStream = null;
HttpURLConnection httpConn = (HttpURLConnection)ucon;
httpConn.setRequestMethod("GET");
httpConn.connect();
if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK)
{
inputStream = httpConn.getInputStream();
}
FileOutputStream fos = new FileOutputStream(file);
int totalSize = httpConn.getContentLength();
int downloadedSize = 0;
byte[] buffer = new byte[1024];
int bufferLength = 0;
while ( (bufferLength = inputStream.read(buffer)) >0 )
{
fos.write(buffer, 0, bufferLength);
downloadedSize += bufferLength;
}
fos.close();
}
catch(IOException io)
{
io.printStackTrace();
}
catch(Exception e)
{
e.printStackTrace();
}
}
@Override
protected void onPostExecute(Bitmap result) {
super.onPostExecute(result);
if(dialog.isShowing()) dialog.dismiss();
File image = new File(sdCardPath, "test.jpg");
if(image.exists()){
Bitmap bitmap = BitmapFactory.decodeFile(image.getAbsolutePath());
//set image
ImageView imageView= (ImageView) findViewById(R.id.imageView1);
imageView.setImageBitmap(bitmap);
//set background
BitmapDrawable bitmapDrawable = new BitmapDrawable(bitmap);
main_layout.setBackgroundDrawable(bitmapDrawable);
} else
Log.d("hello", "onPostExecute: image doesn't exists.");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment