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
classBitmapDownloaderTaskextendsAsyncTask<String, Void, Bitmap> {
privateString url;
private final WeakReference<ImageView> imageViewReference;
publicBitmapDownloaderTask(ImageView imageView) {
imageViewReference = newWeakReference<ImageView>(imageView);
}
@Override// Actual download method, run in the task threadprotectedBitmapdoInBackground(String... params) {
// params comes from the execute() call: params[0] is the url.returndownloadBitmap(params[0]);
}
@Override// Once the image is downloaded, associates it to the imageViewprotectedvoidonPostExecute(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) {
finalAndroidHttpClientclient= AndroidHttpClient.newInstance("Android");
finalHttpGetgetRequest=newHttpGet(url);
try {
HttpResponseresponse= client.execute(getRequest);
finalintstatusCode= response.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
Log.w("ImageDownloader", "Error " + statusCode + " while retrieving bitmap from " + url);
returnnull;
}
finalHttpEntityentity= response.getEntity();
if (entity != null) {
InputStreaminputStream=null;
try {
inputStream = entity.getContent();
finalBitmapbitmap= 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();
}
}
returnnull;
}
Call asynctask as follows
ImageViewmImageView= (ImageView)findViewById(yourImageViewId);
BitmapDownloaderTaskmDownloaderTask=newBitmapDownloaderTask(mImageView);
mDownloaderTask.execute("YourDownloadUrlHere");
Solution 2:
Try This:
public Bitmap getBitmapFromURL(String src) {
try {
java.net.URLurl=newjava.net.URL(src);
HttpURLConnectionconnection= (HttpURLConnection) url
.openConnection();
connection.setDoInput(true);
connection.connect();
InputStreaminput= connection.getInputStream();
BitmapmyBitmap= BitmapFactory.decodeStream(input);
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
returnnull;
}
}
for OutOfMemoryIssue USe:
public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
intwidth= bm.getWidth();
intheight= bm.getHeight();
floatscaleWidth= ((float) newWidth) / width;
floatscaleHeight= ((float) newHeight) / height;
// CREATE A MATRIX FOR THE MANIPULATIONMatrixmatrix=newMatrix();
// RESIZE THE BIT MAP
matrix.postScale(scaleWidth, scaleHeight);
// "RECREATE" THE NEW BITMAPBitmapresizedBitmap= Bitmap.createBitmap(bm, 0, 0, width, height,
matrix, false);
return resizedBitmap;
}
Solution 3:
Do something like this:
privateclassFetchImageTaskextendsAsyncTask<String, Integer, Bitmap> {
@OverrideprotectedBitmapdoInBackground(String... arg0) {
Bitmap b = null;
try {
b = BitmapFactory.decodeStream((InputStream) newURL(arg0[0]).getContent());
}
catch (MalformedURLException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
return b;
}
@OverrideprotectedvoidonPostExecute(Bitmap result) {
if (result != null) {
imgicon.setImageBitmap(result);
}
}
}
then call it like this:
finalImageViewimgicon= (ImageView) convertView.findViewById(R.id.imgicon);
newFetchImageTask().execute("Url/images/"+bitmapname);
Solution 4:
privateclassFetchImageTaskextendsAsyncTask<String, Integer, Bitmap> {
@OverrideprotectedvoiddoInBackground(String... arg0) {
try {
java.net.URLurl=newjava.net.URL(arg0[0]);
HttpURLConnectionconnection= (HttpURLConnection) url
.openConnection();
connection.setDoInput(true);
connection.connect();
InputStreaminput= connection.getInputStream();
BitmapmyBitmap= BitmapFactory.decodeStream(input);
} catch (IOException e) {
e.printStackTrace();
returnnull;
}
returnnull;
}
}
@OverrideprotectedvoidonPostExecute(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:
AQueryandroidQuery=newAQuery(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"