Created
June 5, 2014 05:21
-
-
Save gvsharma/6cdd868bee5b5580ab88 to your computer and use it in GitHub Desktop.
dounloading an image from server and save it in file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Here arg[0] --> is the image url that we have to pass to this asynctask as parameter. | |
public class DownloadImage extends AsyncTask<String, Integer, Bitmap> { | |
@Override | |
protected void onPreExecute() { | |
super.onPreExecute(); | |
} | |
@Override | |
protected Bitmap doInBackground(String... arg0) { | |
return downloadImage(arg0[0]); | |
} | |
protected void onPostExecute(Bitmap image) { | |
if (image != null) { | |
SaveImage(image); | |
System.out.println("bitmap path: "+image); | |
} else { | |
Toast.makeText(getApplicationContext(), "Couldn't get profile pic from your facebook", 1000).show(); | |
} | |
loadServiceToRegisterWithFb(fullname, facebookId, name, email, gender, longi, lati, dev_type, dev_id); | |
pd.dismiss(); | |
} | |
private Bitmap downloadImage(String _url) { | |
URL url; | |
BufferedOutputStream out; | |
InputStream in; | |
BufferedInputStream buf; | |
try { | |
url = new URL(_url); | |
in = url.openStream(); | |
buf = new BufferedInputStream(in); | |
Bitmap bMap = BitmapFactory.decodeStream(buf); | |
if (in != null) { | |
in.close(); | |
} | |
if (buf != null) { | |
buf.close(); | |
} | |
return bMap; | |
} catch (Exception e) { | |
Log.e("Error reading file", e.toString()); | |
} | |
return null; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void SaveImage(Bitmap finalBitmap) { | |
File root = new File(Environment.getExternalStorageDirectory()+ "/Our_Choice/" + "prof_image"); | |
if (!root.exists()) { | |
root.mkdirs(); | |
} | |
String fname = "profile_image.jpg"; | |
File file = new File (root, fname); | |
saved_path_fullscreen = file.getAbsolutePath(); | |
System.out.println("path: "+ saved_path_fullscreen); | |
if (file.exists ()) | |
file.delete (); | |
try { | |
FileOutputStream out = new FileOutputStream(file); | |
finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out); | |
out.flush(); | |
out.close(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment