Image From Url In Android
I am trying to set an image from a web service for which I am using: private class FetchImageTask extends AsyncTask { @Override protected Bit
Solution 1:
From Android-developers blog
Use this Async Task
class BitmapDownloaderTask extends AsyncTask<String, Void, Bitmap> {
private String url;
private final WeakReference<ImageView> imageViewReference;
public BitmapDownloaderTask(ImageView imageView) {
imageViewReference = new WeakReference<ImageView>(imageView);
}
@Override
// Actual download method, run in the task thread
protected Bitmap doInBackground(String... params) {
// params comes from the execute() call: params[0] is the url.
return downloadBitmap(params[0]);
}
@Override
// Once the image is downloaded, associates it to the imageView
protected void onPostExecute(Bitmap bitmap) {
if (isCancelled()) {
bitmap = null;
}
if (imageViewReference != null) {
ImageView imageView = imageViewReference.get();
if (imageView != null) {
imageView.setImageBitmap(bitmap);
}
}
}
}
Use this function for download bitmap from url
static Bitmap downloadBitmap(String url) {
final AndroidHttpClient client = AndroidHttpClient.newInstance("Android");
final HttpGet getRequest = new HttpGet(url);
try {
HttpResponse response = client.execute(getRequest);
final int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
Log.w("ImageDownloader", "Error " + statusCode + " while retrieving bitmap from " + url);
return null;
}
final HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream inputStream = null;
try {
inputStream = entity.getContent();
final Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
return bitmap;
} finally {
if (inputStream != null) {
inputStream.close();
}
entity.consumeContent();
}
}
} catch (Exception e) {
// Could provide a more explicit error message for IOException or IllegalStateException
getRequest.abort();
Log.w("ImageDownloader", "Error while retrieving bitmap from " + url, e.toString());
} finally {
if (client != null) {
client.close();
}
}
return null;
}
Call asynctask as follows
ImageView mImageView = (ImageView)findViewById(yourImageViewId);
BitmapDownloaderTask mDownloaderTask = new BitmapDownloaderTask(mImageView);
mDownloaderTask.execute("YourDownloadUrlHere");
Solution 2:
Try This:
public Bitmap getBitmapFromURL(String src) {
try {
java.net.URL url = new java.net.URL(src);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
for OutOfMemoryIssue USe:
public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
int width = bm.getWidth();
int height = bm.getHeight();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// CREATE A MATRIX FOR THE MANIPULATION
Matrix matrix = new Matrix();
// RESIZE THE BIT MAP
matrix.postScale(scaleWidth, scaleHeight);
// "RECREATE" THE NEW BITMAP
Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height,
matrix, false);
return resizedBitmap;
}
Solution 3:
Do something like this:
private class FetchImageTask extends AsyncTask<String, Integer, Bitmap> {
@Override
protected Bitmap doInBackground(String... arg0) {
Bitmap b = null;
try {
b = BitmapFactory.decodeStream((InputStream) new URL(arg0[0]).getContent());
}
catch (MalformedURLException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
return b;
}
@Override
protected void onPostExecute(Bitmap result) {
if (result != null) {
imgicon.setImageBitmap(result);
}
}
}
then call it like this:
final ImageView imgicon = (ImageView) convertView.findViewById(R.id.imgicon);
new FetchImageTask().execute("Url/images/"+bitmapname);
Solution 4:
private class FetchImageTask extends AsyncTask<String, Integer, Bitmap> {
@Override
protected void doInBackground(String... arg0) {
try {
java.net.URL url = new java.net.URL(arg0[0]);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
} catch (IOException e) {
e.printStackTrace();
return null;
}
return null;
}
}
@Override
protected void onPostExecute(Bitmap result) {
imageview.setImageBitmap(mybitmap);
}
}
Solution 5:
Put this jar into your
libs
folder and right-click on jar and Build Path -> Add to build path
Use it based on this sample:
AQuery androidQuery = new AQuery(this); // make AndroidQuery object
androidQuery
.id(yourImageView)
.image(
imageUrl,
isCacheUrlImageOnMemery,
isCacheUrlImageOnFile);
If true then given URL image cache on memory so after word android query check is given URL image cache on either memory or file then it take from cache other wise it try to getting from URL
isCacheUrlImageOnMemery
Same like isCacheUrlImageOnMemery
but first of all android query check on memory then file in case if we have not much of memory then we cache on file the two option are available.
isCacheUrlImageOnFile
Post a Comment for "Image From Url In Android"