Libgdx getting Image from facebook at runtime - android

I have image-url getting from facebook, I want to display that image at runtine in my game in libgdx.I am using facebook graph api and parse data with help of Json parsing. my approach is as follows:
In main activity
protected void gettingfacebookData() {
try {
JSONArray friendArray = new JSONArray(
prefLevel.getFriendFacebookData());
for (int i = 0; i < friendArray.length(); i++) {
JSONObject jsonObject = friendArray.getJSONObject(i);
String name = jsonObject.getString("name");
String score = jsonObject.getString("score");
String fid = jsonObject.getString("fid");
String image = "http://graph.facebook.com/" + fid
+ "/picture?type=large";
//saving score into array list
PlayingScreen.scoreAl.add(score);
//saving image url into arraylist
PlayingScreen.imageUrlAl.add(image);
} }
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Now how can I display image at run time with this particular image url?

We use a wrapper class, that wraps Texture, for displaying images from web (like facebook profile pictures). This wrapper class takes url of the image and a temporary texture. Just as the wrapper is created, it starts to download image bytes in a background thread. Consumer of this wrapper class simply calls getTexture() to obtain texture and until download has finished, this method returns the temporary texture. When there are bytes available to create a texture getTexture() processes this bytes and starts to return new texture created from url.
Below is a simple version of this wrapper class. Note that, processTextureBytes is called inside getTexture method, not somewhere in background thread. It is because we have to construct texture in the thread which obtains the GLContext. You can add caching and retry mechanisms to this class.
BTW, instead of using http://graph.facebook.com/[uid]/picture url try using one of pic_ urls from FQL. You may want to check this.
public class WebTexture {
private final String url;
private Texture texture;
private volatile byte[] textureBytes;
public WebTexture(String url, Texture tempTexture) {
this.url = url;
texture = tempTexture;
downloadTextureAsync();
}
private void downloadTextureAsync() {
Utils.runInBackground(new Runnable() {
#Override
public void run() {
textureBytes = downloadTextureBytes();
}
});
}
private byte[] downloadTextureBytes() {
try {
return Utils.downloadData(url);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
public Texture getTexture() {
if (textureBytes != null)
processTextureBytes();
return texture;
}
private void processTextureBytes() {
try {
Pixmap pixmap = new Pixmap(textureBytes, 0, textureBytes.length);
Texture gdxTexture = new Texture(pixmap);
gdxTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
texture = gdxTexture;
} catch (Throwable t) {
t.printStackTrace();
} finally {
textureBytes = null;
}
}
}

You should download the image.
Once you have your image as a byte array, you can create a pixmap:
Pixmap pixmap = new Pixmap(imageBytes, 0, imageBytes.length);
Then you can use this pixmap to render the image. For example if are you using scene2d.ui you can set Image's drawable as follows:
image.setDrawable(new TextureRegionDrawable(new TextureRegion(new Texture(profilePicture))));
Hope this helps.

Related

Threads, atomic boolean, synchronized design considerations for a method that has to wait for a result

My code is starting to get a bit hard to debug which leads me to believe that my design choices are not ideal. I am a novice Android programming and would love some help with streamlining the design for optimum operation.
Intro
I am writing an application that uses rfcomm interface to transfer data between a client and server device. The client needs to request certain things from the server using a specific key, then it needs to wait until the server sends the result back.
Current Design
A button press triggers a request for information from the server.
A new thread is started which performs the request.
A key which is a unique integer is converted to a byte array and sent to the server.
Thread has a while loop that is waiting for a specific boolean to flip from false to true indicating a response back from the server.
Information is received on the server side. Server uses key to identify what to do next.
server starts a thread to run some query and gets a jsonString back as a result.
Server sends jsonstring converted to byte array prepended with the same identifying key back to the client.
Client reads message, and sends the byte array to a handling method based on the identifying key.
Handling method stores jsonString to a class variable and then flips the boolean to let the other thread know that the value it was waiting on has been set.
Json string is converted to object on the client side. Something is done with that object.
This code currently correctly sends info to the server, server correctly does search and gets a valid json string result. However, the issue occurs when the server writes its results make to the client. I am getting 20 messages instead of one and none match the search key...
My questions
Am I doing things in an efficient way design wise?
Can I benefit from using synchronized keyword or and Atomic Boolean to make my code more thread safe? How would I go about implementing it?
Is there a max length for converting strings to byte array? Maybe the code is trying to break up the sending for me and that's why I'm getting 20 different results?
Relevant code
public class ClientSpokesmanClass {
private final int searchKey = 2222222; //set the key to some int.
private boolean pendingSearchResults = false;
List<Place> places = new ArrayList<Place>();
private final Handler handler = new Handler(){
#Override
public void handleMessage(Message msg){
switch(msg.what) {
...
case MESSAGE_READ:
//Message received from server
readAndDistribute(msg.arg1, msg.obj);
break;
...
}
}
};
public List<Place> getPlacesFromServer(String query){
//ask server for search results
requestSearchFromServer(query);
//just wait for them...
while (pendingSearchResults){
//just waiting
}
return places;
}
private void requestSearchFromConnectedDevice(String query) {
if (mBluetoothState == STATE_CONNECTED){
byte[] bites = new byte[4];
bites = ByteBuffer.wrap(bites).putInt(searchKey).array();
byte[] stringBytes = null;
try {
stringBytes = query.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
Log.e(TAG, "unsupported encoding", e);
}
int keyLength = bites.length;
int stringLength = stringBytes.length;
byte[] combined = new byte[keyLength+stringLength];
System.arraycopy(bites, 0, combined, 0, keyLength);
System.arraycopy(stringBytes, 0, combined, keyLength, stringLength);
mBluetoothService.write(combined);
}
pendingSearchResults = true;
}
private void receiveSearchResults(byte[] bites){
String jsonString = "";
PlacesJSONParser parser = new PlacesJSONParser();
try {
jsonString = new String(bites, "UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
Log.e(TAG, "unsupported encoding", e);
}
if (D) Log.d(TAG, "Json string we got is "+jsonString);
try {
places = parser.parse(new JSONObject(jsonString));
} catch (JSONException e) {
// TODO Auto-generated catch block
Log.e(TAG, "JSON exception", e);
}
pendingSearchResults = false;
}
/**
* Reads come here first. Then, based on the key prepended to them,
* they then go to other methods for further work.
* #param bytes
* #param buffer
*/
private synchronized void readAndDistribute(int bytes, Object buffer){
byte[] buff = (byte[]) buffer;
int key = ByteBuffer.wrap(Arrays.copyOfRange(buff, 0, 4)).getInt();
if (key == searchKey){
receiveSearchResults(Arrays.copyOfRange(buff, 4, bytes));
}else{
//do something else
}
}
}
.
public class ClientUI extends Activity {
...
onQueryTextSubmit(String query){
final String queryFinal = query;
Thread thread = new Thread(){
public void run() {
places = ClientSpokesmanClass.getPlacesFromServer(query);
doSomethingWithPlaces();
}
};
thread.start();
}
}
.
public class ServerReceive {
private searchKey = 2222222;
...
//code that handles messages, reads key, and then runs doSearchAndWriteResults()
...
private synchronized void doSearchAndWriteResults(byte[] bites){
if (D) Log.d(TAG, "+++writeSearchResults");
//Initialize query and placesString
String query = null;
String placesString;
//Convert byte array to the query string
try {
query = new String(bites, "UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
Log.e(TAG, "unsupported encoding",e);
}
//if the string was converted successfully...
if (query != null){
//Run the places query and set the json string to placesString
if (D) Log.d(TAG, "query is "+query);
PlacesProvider placeProvider = new PlacesProvider();
placesString = placeProvider.getPlacesString(query);
}
//initialize a bite array
byte[] stringBytes = null;
try {
//convert jsonString to byte array
stringBytes = placesString.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
Log.e(TAG, "unsupported encoding",e);
}
//Put the search key to a byte array. I am using this key on the client side
//to confirm that we are reading searchResults and not some other type of write.
byte[] bite = new byte[4];
bite = ByteBuffer.wrap(bite).putInt(searchKey).array();
//Get the lengths of the two byte arrays
int keyLength = bite.length;
int stringLength = stringBytes.length;
//combine the byte arrays for sending
byte[] combined = new byte[keyLength+stringLength];
System.arraycopy(bite, 0, combined, 0, keyLength);
System.arraycopy(stringBytes, 0, combined, keyLength, stringLength);
if (D) Log.d(TAG, "Actually writing things here...");
//send the byte arrrays over rfcomm
mBluetoothService.write(combined);
}
}
Take a look at https://github.com/gettyimages/connect_sdk_java. Specifically, at the test application. It performs a search using an AsyncTask and the private class notifies the UI via onPostExecute. Hopefully, this will get you further along.

How to load quickly the image from the url in the ImageView of the normal android view(not list view)?

My internet connection is very fast. Despite that my image loads very slow in the app. Here is the code i have been using. As suggested I have been using async method to perform certain task seperately..
public class ItemPage extends Activity{
ImageView image;
String url;
#Override
protected void onCreate(Bundle savedInstanceState) {
image = (ImageView) findViewById(R.id.img);
//getting url from the parent activity
Intent intent = getIntent();
Bundle extras = intent.getExtras();
if(extras != null) url = extras.getString("bigurl");
//async call
new DownloadFilesTask().execute();
}
private class DownloadFilesTask extends AsyncTask<Void, Void, Void> { //working part
Drawable drawable;
#Override
protected Void doInBackground(Void... params) {
try {
drawable =drawableFromUrl(url);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Void result) {
image.setImageDrawable(drawable);
}
}
public static Drawable drawableFromUrl(String url) throws IOException {
Bitmap x;
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
connection.connect();
InputStream input = connection.getInputStream();
x = BitmapFactory.decodeStream(input);
return new BitmapDrawable(x);
}
}
Please help me. Is there any way to load the image faster from the url? Thanks in advance for your time.
i think you want to use Aquery Library .
it is load images very speedly.
refer this link
This works for me.
Bitmap mIcon_val;
URL newurl;
private void loadImage() {
Thread backgroundThread = new Thread() {
#Override
public void run() {
// do second step
try {
newurl = new URL(image_url);
mIcon_val = BitmapFactory.decodeStream(newurl
.openConnection().getInputStream());
} catch (MalformedURLException e) {
} catch (IOException e) {
}
Message msg = Message.obtain();
msg.what = 123;
handler.sendMessage(msg);
}
};
}
private Handler handler = new Handler() {
#Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 123:
imageView.setImageBitmap(mIcon_val);
break;
}
}
};
For calling the thread, you can use
backgroundThread.start();
My internet connection is very fast. Despite that my image loads very slow in the app.
It doesnt mean you can download image very fast when you have a really fast connection, it could be that the web server have a limit download speed for each connection, or the server is far from your current location. I would recommend to check the download speed of the server you are trying to connection first, if it is slow then it is not code but the server itself.
you may try using picasso library.it has several functions to compress,crop images that may help in downloading images at better rates.the link provides the full documentation of usage of the library-
http://square.github.io/picasso/
Here's something you can do to speed up the image processing side of things.
When you decode the stream you are converting it to an in-memory raster format. A 32bit image at 500x500 will take 1 mb of memory (500x500x4bytes). the native pixel format varies by device, and the fastest performance is to match the bitmap's pixel format to the system's pixel format so the system doesn't need to convert the image from it's native format to the window format.
Older devices in particular will use the 16 bit format. The image would be stored at 32bits, and then converted to 16bits when it's drawn to the screen. On these devices, you would be saving half the memory storage and avoiding a costly pixelwise conversion by decoding the image at 16 bits instead of at 32 bits. On newer devices that have the memory, it's better to store the image at 32 bits to avoid the conversion penalty.
So somewhere early on, store the pixel format that is used by the system. It will either be a 16 bit format (RGB_565) or a 32 bit format (8bits per pixel per color)
// default to 32 bits per pixel
Config bitmapConfig = Bitmap.Config.ARGB_8888 ; //RGB_565 | ARGB_8888
WindowManager wm = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
int bmp_cfg = wm.getDefaultDisplay().getPixelFormat();
if (bmp_cfg == PixelFormat.RGBA_8888 || bmp_cfg == PixelFormat.RGBX_8888 || bmp_cfg == PixelFormat.RGB_888){
bitmapConfig = Bitmap.Config.ARGB_8888;
} else {
bitmapConfig = Bitmap.Config.RGB_565;
}
Later, when you are decoding the bitmap, use the window manager's default pixel format so that it will create the bitmap with the correct pixel format while decoding (rather than converting in a second pass)
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = bitmapConfig; // match pixel format to widnow system's format
Bitmap bitmap = BitmapFactory.decodeStream(input, null, options);
bitmap = Bitmap.createBitmap(bitmap);
You can also play with the sample size to adjust the bitmap, but at 500x500 you probably need the whole image anyway.

Uploading images to server from Android device

We are currently in need of a tool to upload photos shot from an android device directly to our server without creating an android app. Can this be done in a web application? I already tried getusermedia API without success.
Now you can make this task simpler and more useful i have designed one class named ImageUploaderUtility, which uploads images to a remote webserver. ImageUploadUtility has simple methods where you just need to pass the name of your image to upload.
Apart from this the key point here is this class needs to be executed in an AsynTask so it ensures the whole image upload process doesn't freeze your UI.
I have used this same past project and expanded it.
The first thing to understand is i have made inner class in my BlogPostExampleActivity which is extended from AsyncTask, this inner class will call the method to upload image which is declared and defined in ImageUploader Utility that's all isn't that simple.
Have a look at the code and let me know if you can't understand anything in this.
private class ImageUploaderTask extends AsyncTask<String, Integer, Void>{
#Override
protected void onPreExecute()
{
simpleWaitDialog = ProgressDialog.show(BlogPostExamplesActivity.this, "Wait", "Uploading Image");
}
#Override
protected Void doInBackground(String... params){
new ImageUploadUtility().uploadSingleImage(params[0]);
return null;
}
#Override
protected void onPostExecute(Void result){
simpleWaitDialog.dismiss();
}
}
And Now ImageUploaderUtility.java
package gargi.blogpostexamples.webservice;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import org.apache.commons.httpclient.methods.PostMethod;import org.apache.commons.httpclient.methods.multipart.ByteArrayPartSource;import org.apache.commons.httpclient.methods.multipart.FilePart;import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;import org.apache.commons.httpclient.methods.multipart.Part;import org.apache.commons.httpclient.methods.multipart.StringPart;import android.os.Environment;import android.util.Log;class ImageUploadUtility {/** * Simple Utility Method gets called from other class to start uploading the image * #param fileNameToUpload name of the file to upload */public void uploadSingleImage(String fileNameToUpload){ try { doUploadinBackground(getBytesFromFile(new File(Environment.getExternalStorageDirectory(),fileNameToUpload)), fileNameToUpload); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }}/** * Method uploads the image using http multipart form data. * We are not using the default httpclient coming with android we are using the new from apache * they are placed in libs folder of the application * * #param imageData * #param filename * #return * #throws Exception */static boolean doUploadinBackground(final byte[] imageData, String filename) throws Exception{ String responseString = null; PostMethod method; method = new PostMethod("your url to upload"); org.apache.commons.httpclient.HttpClient client = new org.apache.commons.httpclient.HttpClient(); client.getHttpConnectionManager().getParams().setConnectionTimeout( 100000); FilePart photo = new FilePart("userfile", new ByteArrayPartSource( filename, imageData)); photo.setContentType("image/jpeg"); photo.setCharSet(null); String s = new String(imageData); Part[] parts = { new StringPart("latitude", "123456"), new StringPart("longitude","12.123567"), new StringPart("imei","1234567899"), new StringPart("to_email","some email"), photo }; method.setRequestEntity(new MultipartRequestEntity(parts, method .getParams())); client.executeMethod(method); responseString = method.getResponseBodyAsString(); method.releaseConnection(); Log.e("httpPost", "Response status: " + responseString); if (responseString.equals("SUCCESS")) { return true; } else { return false; } }/** * Simple Reads the image file and converts them to Bytes * * #param file name of the file * #return byte array which is converted from the image * #throws IOException */public static byte[] getBytesFromFile(File file) throws IOException { InputStream is = new FileInputStream(file); // Get the size of the file long length = file.length(); // You cannot create an array using a long type. // It needs to be an int type. // Before converting to an int type, check // to ensure that file is not larger than Integer.MAX_VALUE. if (length > Integer.MAX_VALUE) { // File is too large } // Create the byte array to hold the data byte[] bytes = new byte[(int)length]; // Read in the bytes int offset = 0; int numRead = 0; while (offset < bytes.length && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) { offset += numRead; } // Ensure all the bytes have been read in if (offset < bytes.length) { throw new IOException("Could not completely read file "+file.getName()); } // Close the input stream and return bytes is.close(); return bytes;}}
So that's it for now, the bad thing in this is i have used to Inner Class in this activity and i don't know why but i am not feeling good about that, so next time i will try to make a single class extended from AysncTask and use that in existing examples.
Apart from this if you feel it can be improved in some other ways please don't forget to leave that in comment.
Last but not the list you can download the complete example from this link.

How does Android memory allocation work with ImageView?

I'm loading approximately 20 pictures of size 50-70K from server, then display them in a ListView. Initially I stored the data as Bitmap which causes memory running out quickly. Then I decided to compress all these bitmap files and store in Content provider Media. So in my adapter, the user data only contains the Uri to the image file.
However it didn't fix the problem at all, it run a bit longer, but still crashed after loading about 10 pictures or so. Here is the error log from the compiler.
1048576-byte external allocation too large for this process
VM won't let us allocate 1048576 bytes
I even clean up each bitmap data after setting it to my ImageView, plus delete the all the image files which are stored in my sdcard
#Override
public void onDestroy() {
// clean up
for (User user : userList) {
getContentResolver().delete(user.getImageUri(), null, null);
}
super.onDestroy();
}
private Uri constructUriFromBitmap(Bitmap bitmap) {
ContentValues values = new ContentValues(1);
values.put(Media.MIME_TYPE, "image/jpeg");
Uri uri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values);
try {
OutputStream outStream = getContentResolver().openOutputStream(uri);
bitmap.compress(Bitmap.CompressFormat.JPEG, 50, outStream);
outStream.close();
}
catch (Exception e) {
Log.e(TAG, "exception while writing image", e);
}
bitmap.recycle();
return uri;
}
Now I ran out of idea, I really don't know what could go wrong in this case. I wonder if anyone has experienced this issue could shed me some lights?
Since the code is quite long, I only extract the main functions:
Here is my User class data:
public class FriendFeed {
// required parameters
private final int activityId; // in case we want to handle the detail of
// this activity
private final int friendId;
private final String friendName;
private final Challenge.Type challengeType;
private final String activityTime;
private final String placeName;
// optional parameter
private String challengeName;
private String challengeDescription;
private Uri activitySnapPictureUri = null;
private Uri friendPictureUri = null;
private String activityComment;
And here is my main function:
#Override
protected Boolean doInBackground(Void...voids) {
JSONArray array = JsonHelper.getJsonArrayFromUrlWithData(GET_FRIEND_FEED_URL, datas);
if (array != null) {
try {
for (int i = 0; i < array.length(); ++i) {
Uri snapPictureUri = null;
Uri userPictureUri = null;
if (Challenge.returnType(array.getJSONObject(i).getString("challenges_tbl_type")) == Challenge.Type.SNAP_PICTURE) {
snapPictureUri = constructUriFromBitmap(ImageHelper.downloadImage(array.getJSONObject(i).getString("activity_tbl_snap_picture_url")));
}
if(ImageHelper.downloadImage(array.getJSONObject(i).getString("users_tbl_user_image_url")) != null) {
userPictureUri = constructUriFromBitmap(ImageHelper.downloadImage(array.getJSONObject(i).getString("users_tbl_user_image_url")));
}
publishProgress(
new FriendFeed.Builder(
// required parameters
array.getJSONObject(i).getInt("activity_tbl_id"),
array.getJSONObject(i).getInt("friends_tbl_friend_id"),
array.getJSONObject(i).getString("users_tbl_username"),
Challenge.returnType(array.getJSONObject(i).getString("challenges_tbl_type")),
array.getJSONObject(i).getString("activity_tbl_created"),
array.getJSONObject(i).getString("spots_tbl_name"))
// optional parameters
.challengeName(array.getJSONObject(i).getString("challenges_tbl_name"))
.challengeDescription(array.getJSONObject(i).getString("challenges_tbl_description"))
.activitySnapPictureUri(snapPictureUri)
.friendPictureUri(userPictureUri)
.activityComment(array.getJSONObject(i).getString("activity_tbl_comment"))
.build());
}
}
catch (JSONException e) {
Log.e(TAG + "GetFriendFeedTask.doInBackGround(Void ...voids) : ", "JSON error parsing data" + e.toString());
}
return true;
}
else {
return false;
}
}
Android enforces a per-process memory allocation limit of 24MB so you can't allocate more than that. However, 20 pics of 70K each should amount to 1.4MB only... so my guesses:
Maybe you're allocating Bitmaps in other parts of your app, so that there's less than 1.4MB available for your bitmaps on this ListView.
Memory leak somewhere
If you determine that you really need all the bitmaps you're using, are you sure you need the bitmaps to be this large or have this much resolution? Reducing them can help.
If all else fails and you do need lots of bitmaps in memory, you can always use OpenGL textures.

How to lazy load images in ListView in Android

I am using a ListView to display some images and captions associated with those images. I am getting the images from the Internet. Is there a way to lazy load images so while the text displays, the UI is not blocked and images are displayed as they are downloaded?
The total number of images is not fixed.
Here's what I created to hold the images that my app is currently displaying. Please note that the "Log" object in use here is my custom wrapper around the final Log class inside Android.
package com.wilson.android.library;
/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
import java.io.IOException;
public class DrawableManager {
private final Map<String, Drawable> drawableMap;
public DrawableManager() {
drawableMap = new HashMap<String, Drawable>();
}
public Drawable fetchDrawable(String urlString) {
if (drawableMap.containsKey(urlString)) {
return drawableMap.get(urlString);
}
Log.d(this.getClass().getSimpleName(), "image url:" + urlString);
try {
InputStream is = fetch(urlString);
Drawable drawable = Drawable.createFromStream(is, "src");
if (drawable != null) {
drawableMap.put(urlString, drawable);
Log.d(this.getClass().getSimpleName(), "got a thumbnail drawable: " + drawable.getBounds() + ", "
+ drawable.getIntrinsicHeight() + "," + drawable.getIntrinsicWidth() + ", "
+ drawable.getMinimumHeight() + "," + drawable.getMinimumWidth());
} else {
Log.w(this.getClass().getSimpleName(), "could not get thumbnail");
}
return drawable;
} catch (MalformedURLException e) {
Log.e(this.getClass().getSimpleName(), "fetchDrawable failed", e);
return null;
} catch (IOException e) {
Log.e(this.getClass().getSimpleName(), "fetchDrawable failed", e);
return null;
}
}
public void fetchDrawableOnThread(final String urlString, final ImageView imageView) {
if (drawableMap.containsKey(urlString)) {
imageView.setImageDrawable(drawableMap.get(urlString));
}
final Handler handler = new Handler(Looper.getMainLooper()) {
#Override
public void handleMessage(Message message) {
imageView.setImageDrawable((Drawable) message.obj);
}
};
Thread thread = new Thread() {
#Override
public void run() {
//TODO : set imageView to a "pending" image
Drawable drawable = fetchDrawable(urlString);
Message message = handler.obtainMessage(1, drawable);
handler.sendMessage(message);
}
};
thread.start();
}
private InputStream fetch(String urlString) throws MalformedURLException, IOException {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet request = new HttpGet(urlString);
HttpResponse response = httpClient.execute(request);
return response.getEntity().getContent();
}
}
I made a simple demo of a lazy list (located at GitHub) with images.
Basic Usage
ImageLoader imageLoader=new ImageLoader(context); ...
imageLoader.DisplayImage(url, imageView);
Don't forget to add the
following permissions to your AndroidManifest.xml:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> Please
create only one instance of ImageLoader and reuse it all around your
application. This way image caching will be much more efficient.
It may be helpful to somebody. It downloads images in the background thread. Images are being cached on an SD card and in memory. The cache implementation is very simple and is just enough for the demo. I decode images with inSampleSize to reduce memory consumption. I also try to handle recycled views correctly.
I recommend open source instrument Universal Image Loader. It is originally based on Fedor Vlasov's project LazyList and has been vastly improved since then.
Multithread image loading
Possibility of wide tuning ImageLoader's configuration (thread executors, downloader, decoder, memory and disc cache, display image options, and others)
Possibility of image caching in memory and/or on the device's file system (or SD card)
Possibility to "listen" loading process
Possibility to customize every display image call with separated options
Widget support
Android 2.0+ support
Multithreading For Performance, a tutorial by Gilles Debunne.
This is from the Android Developers Blog. The suggested code uses:
AsyncTasks.
A hard, limited size, FIFO cache.
A soft, easily garbage collect-ed cache.
A placeholder Drawable while you download.
Update: Note that this answer is pretty ineffective now. The Garbage Collector acts aggressively on SoftReference and WeakReference, so this code is NOT suitable for new apps. (Instead, try libraries like Universal Image Loader suggested in other answers.)
Thanks to James for the code, and Bao-Long for the suggestion of using SoftReference. I implemented the SoftReference changes on James' code. Unfortunately, SoftReferences caused my images to be garbage collected too quickly. In my case, it was fine without the SoftReference stuff, because my list size is limited and my images are small.
There's a discussion from a year ago regarding the SoftReferences on google groups: link to thread. As a solution to the too-early garbage collection, they suggest the possibility of manually setting the VM heap size using dalvik.system.VMRuntime.setMinimumHeapSize(), which is not very attractive to me.
public DrawableManager() {
drawableMap = new HashMap<String, SoftReference<Drawable>>();
}
public Drawable fetchDrawable(String urlString) {
SoftReference<Drawable> drawableRef = drawableMap.get(urlString);
if (drawableRef != null) {
Drawable drawable = drawableRef.get();
if (drawable != null)
return drawable;
// Reference has expired so remove the key from drawableMap
drawableMap.remove(urlString);
}
if (Constants.LOGGING) Log.d(this.getClass().getSimpleName(), "image url:" + urlString);
try {
InputStream is = fetch(urlString);
Drawable drawable = Drawable.createFromStream(is, "src");
drawableRef = new SoftReference<Drawable>(drawable);
drawableMap.put(urlString, drawableRef);
if (Constants.LOGGING) Log.d(this.getClass().getSimpleName(), "got a thumbnail drawable: " + drawable.getBounds() + ", "
+ drawable.getIntrinsicHeight() + "," + drawable.getIntrinsicWidth() + ", "
+ drawable.getMinimumHeight() + "," + drawable.getMinimumWidth());
return drawableRef.get();
} catch (MalformedURLException e) {
if (Constants.LOGGING) Log.e(this.getClass().getSimpleName(), "fetchDrawable failed", e);
return null;
} catch (IOException e) {
if (Constants.LOGGING) Log.e(this.getClass().getSimpleName(), "fetchDrawable failed", e);
return null;
}
}
public void fetchDrawableOnThread(final String urlString, final ImageView imageView) {
SoftReference<Drawable> drawableRef = drawableMap.get(urlString);
if (drawableRef != null) {
Drawable drawable = drawableRef.get();
if (drawable != null) {
imageView.setImageDrawable(drawableRef.get());
return;
}
// Reference has expired so remove the key from drawableMap
drawableMap.remove(urlString);
}
final Handler handler = new Handler() {
#Override
public void handleMessage(Message message) {
imageView.setImageDrawable((Drawable) message.obj);
}
};
Thread thread = new Thread() {
#Override
public void run() {
//TODO : set imageView to a "pending" image
Drawable drawable = fetchDrawable(urlString);
Message message = handler.obtainMessage(1, drawable);
handler.sendMessage(message);
}
};
thread.start();
}
Picasso
Use Jake Wharton's Picasso Library.
(A Perfect ImageLoading Library from the developer of ActionBarSherlock)
A powerful image downloading and caching library for Android.
Images add much-needed context and visual flair to Android applications. Picasso allows for hassle-free image loading in your application—often in one line of code!
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
Many common pitfalls of image loading on Android are handled automatically by Picasso:
Handling ImageView recycling and download cancellation in an adapter.
Complex image transformations with minimal memory use.
Automatic memory and disk caching.
Picasso Jake Wharton's Library
Glide
Glide is a fast and efficient open-source media management framework for Android that wraps media decoding, memory and disk caching, and resource pooling into a simple and easy-to-use interface.
Glide supports fetching, decoding, and displaying video stills, images, and animated GIFs. Glide includes a flexible API that allows developers to plug into almost any network stack. By default, Glide uses a custom HttpUrlConnection based stack but also includes utility libraries plug-in to Google's Volley project or Square's OkHttp library instead.
Glide.with(this).load("your-url-here").into(imageView);
Glide's primary focus is on making scrolling any kind of a list of images as smooth and fast as possible, but Glide is also effective for almost any case where you need to fetch, resize, and display a remote image.
Glide Image Loading Library
Fresco by Facebook
Fresco is a powerful system for displaying images in Android applications.
Fresco takes care of image loading and display, so you don't have to. It will load images from the network, local storage, or local resources, and display a placeholder until the image has arrived. It has two levels of cache; one in memory and another in internal storage.
Fresco Github
In Android 4.x and lower, Fresco puts images in a special region of Android memory. This lets your application run faster - and suffer the dreaded OutOfMemoryError much less often.
Fresco Documentation
High-performance loader - after examining the methods suggested here,
I used Ben's solution with some changes -
I realized that working with drawable is faster than with bitmaps so I uses drawable instead
Using SoftReference is great, but it makes the cached image to be deleted too often, so I added a Linked list that holds images references, preventing the image to be deleted, until it reached a predefined size
To open the InputStream I used java.net.URLConnection which allows me to use web cache (you need to set a response cache first, but that's another story)
My code:
import java.util.Map;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Collections;
import java.util.WeakHashMap;
import java.lang.ref.SoftReference;
import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;
import android.graphics.drawable.Drawable;
import android.widget.ImageView;
import android.os.Handler;
import android.os.Message;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
public class DrawableBackgroundDownloader {
private final Map<String, SoftReference<Drawable>> mCache = new HashMap<String, SoftReference<Drawable>>();
private final LinkedList <Drawable> mChacheController = new LinkedList <Drawable> ();
private ExecutorService mThreadPool;
private final Map<ImageView, String> mImageViews = Collections.synchronizedMap(new WeakHashMap<ImageView, String>());
public static int MAX_CACHE_SIZE = 80;
public int THREAD_POOL_SIZE = 3;
/**
* Constructor
*/
public DrawableBackgroundDownloader() {
mThreadPool = Executors.newFixedThreadPool(THREAD_POOL_SIZE);
}
/**
* Clears all instance data and stops running threads
*/
public void Reset() {
ExecutorService oldThreadPool = mThreadPool;
mThreadPool = Executors.newFixedThreadPool(THREAD_POOL_SIZE);
oldThreadPool.shutdownNow();
mChacheController.clear();
mCache.clear();
mImageViews.clear();
}
public void loadDrawable(final String url, final ImageView imageView,Drawable placeholder) {
mImageViews.put(imageView, url);
Drawable drawable = getDrawableFromCache(url);
// check in UI thread, so no concurrency issues
if (drawable != null) {
//Log.d(null, "Item loaded from mCache: " + url);
imageView.setImageDrawable(drawable);
} else {
imageView.setImageDrawable(placeholder);
queueJob(url, imageView, placeholder);
}
}
private Drawable getDrawableFromCache(String url) {
if (mCache.containsKey(url)) {
return mCache.get(url).get();
}
return null;
}
private synchronized void putDrawableInCache(String url,Drawable drawable) {
int chacheControllerSize = mChacheController.size();
if (chacheControllerSize > MAX_CACHE_SIZE)
mChacheController.subList(0, MAX_CACHE_SIZE/2).clear();
mChacheController.addLast(drawable);
mCache.put(url, new SoftReference<Drawable>(drawable));
}
private void queueJob(final String url, final ImageView imageView,final Drawable placeholder) {
/* Create handler in UI thread. */
final Handler handler = new Handler() {
#Override
public void handleMessage(Message msg) {
String tag = mImageViews.get(imageView);
if (tag != null && tag.equals(url)) {
if (imageView.isShown())
if (msg.obj != null) {
imageView.setImageDrawable((Drawable) msg.obj);
} else {
imageView.setImageDrawable(placeholder);
//Log.d(null, "fail " + url);
}
}
}
};
mThreadPool.submit(new Runnable() {
#Override
public void run() {
final Drawable bmp = downloadDrawable(url);
// if the view is not visible anymore, the image will be ready for next time in cache
if (imageView.isShown())
{
Message message = Message.obtain();
message.obj = bmp;
//Log.d(null, "Item downloaded: " + url);
handler.sendMessage(message);
}
}
});
}
private Drawable downloadDrawable(String url) {
try {
InputStream is = getInputStream(url);
Drawable drawable = Drawable.createFromStream(is, url);
putDrawableInCache(url,drawable);
return drawable;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private InputStream getInputStream(String urlString) throws MalformedURLException, IOException {
URL url = new URL(urlString);
URLConnection connection;
connection = url.openConnection();
connection.setUseCaches(true);
connection.connect();
InputStream response = connection.getInputStream();
return response;
}
}
I have followed this Android Training and I think it does an excellent job at downloading images without blocking the main UI. It also handles caching and dealing with scrolling through many images: Loading Large Bitmaps Efficiently
1. Picasso allows for hassle-free image loading in your application—often in one line of code!
Use Gradle:
implementation 'com.squareup.picasso:picasso:(insert latest version)'
Just one line of code!
Picasso.get().load("http://i.imgur.com/DvpvklR.png").into(imageView);
2. Glide An image loading and caching library for Android focused on smooth scrolling
Use Gradle:
repositories {
mavenCentral()
google()
}
dependencies {
implementation 'com.github.bumptech.glide:glide:4.11.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'
}
// For a simple view:
Glide.with(this).load("http://i.imgur.com/DvpvklR.png").into(imageView);
3. fresco is a powerful system for displaying images in Android
applications.Fresco takes care of image loading and display, so you don't have
to.
Getting Started with Fresco
I've written a tutorial that explains how to do lazy-loading of images in a listview. I go into some detail about the issues of recycling and concurrency. I also use a fixed thread pool to prevent spawning a lot of threads.
Lazy loading of images in Listview Tutorial
The way I do it is by launching a thread to download the images in the background and hand it a callback for each list item. When an image is finished downloading it calls the callback which updates the view for the list item.
This method doesn't work very well when you're recycling views however.
I just want to add one more good example, XML Adapters. As it's is used by Google and I am also using the same logic to avoid an OutOfMemory error.
Basically this ImageDownloader is your answer (as it covers most of your requirements). Some you can also implement in that.
This is a common problem on Android that has been solved in many ways by many people. In my opinion the best solution I've seen is the relatively new library called Picasso. Here are the highlights:
Open source, but headed up by Jake Wharton of ActionBarSherlock fame.
Asynchronously load images from network or app resources with one line of code
Automatic ListView detection
Automatic disk and memory caching
Can do custom transformations
Lots of configurable options
Super simple API
Frequently updated
I have been using NetworkImageView from the new Android Volley Library com.android.volley.toolbox.NetworkImageView, and it seems to be working pretty well. Apparently, this is the same view that is used in Google Play and other new Google applications. Definitely worth checking out.
Google I/O 2013 volley image cache tutorial
Developers Google events
Well, image loading time from the Internet has many solutions. You may also use the library Android-Query. It will give you all the required activity. Make sure what you want to do and read the library wiki page. And solve the image loading restriction.
This is my code:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.row, null);
}
ImageView imageview = (ImageView) v.findViewById(R.id.icon);
AQuery aq = new AQuery(convertView);
String imageUrl = "http://www.vikispot.com/z/images/vikispot/android-w.png";
aq.id(imageview).progress(this).image(imageUrl, true, true, 0, 0, new BitmapAjaxCallback() {
#Override
public void callback(String url, ImageView iv, Bitmap bm, AjaxStatus status) {
iv.setImageBitmap(bm);
}
));
return v;
}
It should be solve your lazy loading problem.
I think this issue is very popular among Android developers, and there are plenty of such libraries that claims to resolve this issue, but only a few of them seems to be on the mark. AQuery is one such library, but it is better than most of them in all aspects and is worth trying for.
You must try this Universal Loader is best.
I am using this after done many RnD on lazy loading .
Universal Image Loader
Features
Multithread image loading (async or sync)
Wide customization of ImageLoader's configuration (thread executors, downloader, decoder, memory and disk cache, display image options, etc.)
Many customization options for every display image call (stub images, caching switch, decoding options, Bitmap processing and displaying, etc.)
Image caching in memory and/or on disk (device's file system or SD card)
Listening loading process (including downloading progress)
Android 2.0+ support
Have a look at Shutterbug, Applidium's lightweight SDWebImage (a nice library on iOS) port to Android.
It supports asynchronous caching, stores failed URLs, handles concurrency well, and helpful subclasses are included.
Pull requests (and bug reports) are welcome, too!
DroidParts has ImageFetcher that requires zero configuration to get started.
Uses a disk & in-memory Least Recently Used (LRU) cache.
Efficiently decodes images.
Supports modifying bitmaps in background thread.
Has simple cross-fade.
Has image loading progress callback.
Clone DroidPartsGram for an example:
Novoda also has a great lazy image loading library and many apps like Songkick, Podio, SecretDJ and ImageSearch use their library.
Their library is hosted here on Github and they have a pretty active issues tracker as well. Their project seems to be pretty active too, with over 300+ commits at the time of writing this reply.
Just a quick tip for someone who is in indecision regarding what library to use for lazy-loading images:
There are four basic ways.
DIY => Not the best solution but for a few images and if you want to go without the hassle of using others libraries
Volley's Lazy Loading library => From guys at android. It is nice and everything but is poorly documented and hence is a problem to use.
Picasso: A simple solution that just works, you can even specify the exact image size you want to bring in. It is very simple to use but might not be very "performant" for apps that has to deal with humongous amounts of images.
UIL: The best way to lazy load images. You can cache images(you need permission of course), initialize the loader once, then have your work done. The most mature asynchronous image loading library I have ever seen so far.
If you want to display Shimmer layout like Facebook there is a official facebook library for that. FaceBook Shimmer Android
It takes care of everything, You just need to put your desired design code in nested manner in shimmer frame.
Here is a sample code.
<com.facebook.shimmer.ShimmerFrameLayout
android:id=“#+id/shimmer_view_container”
android:layout_width=“wrap_content”
android:layout_height="wrap_content"
shimmer:duration="1000">
<here will be your content to display />
</com.facebook.shimmer.ShimmerFrameLayout>
And here is the java code for it.
ShimmerFrameLayout shimmerContainer = (ShimmerFrameLayout) findViewById(R.id.shimmer_view_container);
shimmerContainer.startShimmerAnimation();
Add this dependency in your gradle file.
implementation 'com.facebook.shimmer:shimmer:0.1.0#aar'
Here is how it looks like.
Check my fork of LazyList. Basically, I improve the LazyList by delaying the call of the ImageView and create two methods:
When you need to put something like "Loading image..."
When you need to show the downloaded image.
I also improved the ImageLoader by implementing a singleton in this object.
All above code have their own worth but with my personal experience just give a try with Picasso.
Picasso is a library specifically for this purpose, in-fact it will manage cache and all other network operations automatically.You will have to add library in your project and just write a single line of code to load image from remote URL.
Please visit here : http://code.tutsplus.com/tutorials/android-sdk-working-with-picasso--cms-22149
Use the glide library. It worked for me and will work for your code too.It works for both images as well as gifs too.
ImageView imageView = (ImageView) findViewById(R.id.test_image);
GlideDrawableImageViewTarget imagePreview = new GlideDrawableImageViewTarget(imageView);
Glide
.with(this)
.load(url)
.listener(new RequestListener<String, GlideDrawable>() {
#Override
public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
return false;
}
#Override
public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
return false;
}
})
.into(imagePreview);
}
I can recommend a different way that works like a charm: Android Query.
You can download that JAR file from here
AQuery androidAQuery = new AQuery(this);
As an example:
androidAQuery.id(YOUR IMAGEVIEW).image(YOUR IMAGE TO LOAD, true, true, getDeviceWidth(), ANY DEFAULT IMAGE YOU WANT TO SHOW);
It's very fast and accurate, and using this you can find many more features like animation when loading, getting a bitmap (if needed), etc.
Give Aquery a try. It has amazingly simple methods to load and cache images asynchronously.
URLImageViewHelper is an amazing library that helps you to do that.
public class ImageDownloader {
Map<String, Bitmap> imageCache;
public ImageDownloader() {
imageCache = new HashMap<String, Bitmap>();
}
// download function
public void download(String url, ImageView imageView) {
if (cancelPotentialDownload(url, imageView)) {
// Caching code right here
String filename = String.valueOf(url.hashCode());
File f = new File(getCacheDirectory(imageView.getContext()),
filename);
// Is the bitmap in our memory cache?
Bitmap bitmap = null;
bitmap = (Bitmap) imageCache.get(f.getPath());
if (bitmap == null) {
bitmap = BitmapFactory.decodeFile(f.getPath());
if (bitmap != null) {
imageCache.put(f.getPath(), bitmap);
}
}
// No? download it
if (bitmap == null) {
try {
BitmapDownloaderTask task = new BitmapDownloaderTask(
imageView);
DownloadedDrawable downloadedDrawable = new DownloadedDrawable(
task);
imageView.setImageDrawable(downloadedDrawable);
task.execute(url);
} catch (Exception e) {
Log.e("Error==>", e.toString());
}
} else {
// Yes? set the image
imageView.setImageBitmap(bitmap);
}
}
}
// cancel a download (internal only)
private static boolean cancelPotentialDownload(String url,
ImageView imageView) {
BitmapDownloaderTask bitmapDownloaderTask = getBitmapDownloaderTask(imageView);
if (bitmapDownloaderTask != null) {
String bitmapUrl = bitmapDownloaderTask.url;
if ((bitmapUrl == null) || (!bitmapUrl.equals(url))) {
bitmapDownloaderTask.cancel(true);
} else {
// The same URL is already being downloaded.
return false;
}
}
return true;
}
// gets an existing download if one exists for the imageview
private static BitmapDownloaderTask getBitmapDownloaderTask(
ImageView imageView) {
if (imageView != null) {
Drawable drawable = imageView.getDrawable();
if (drawable instanceof DownloadedDrawable) {
DownloadedDrawable downloadedDrawable = (DownloadedDrawable) drawable;
return downloadedDrawable.getBitmapDownloaderTask();
}
}
return null;
}
// our caching functions
// Find the dir to save cached images
private static File getCacheDirectory(Context context) {
String sdState = android.os.Environment.getExternalStorageState();
File cacheDir;
if (sdState.equals(android.os.Environment.MEDIA_MOUNTED)) {
File sdDir = android.os.Environment.getExternalStorageDirectory();
// TODO : Change your diretcory here
cacheDir = new File(sdDir, "data/ToDo/images");
} else
cacheDir = context.getCacheDir();
if (!cacheDir.exists())
cacheDir.mkdirs();
return cacheDir;
}
private void writeFile(Bitmap bmp, File f) {
FileOutputStream out = null;
try {
out = new FileOutputStream(f);
bmp.compress(Bitmap.CompressFormat.PNG, 80, out);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null)
out.close();
} catch (Exception ex) {
}
}
}
// download asynctask
public 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.
url = (String) params[0];
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();
BitmapDownloaderTask bitmapDownloaderTask = getBitmapDownloaderTask(imageView);
// Change bitmap only if this process is still associated with
// it
if (this == bitmapDownloaderTask) {
imageView.setImageBitmap(bitmap);
// cache the image
String filename = String.valueOf(url.hashCode());
File f = new File(
getCacheDirectory(imageView.getContext()), filename);
imageCache.put(f.getPath(), bitmap);
writeFile(bitmap, f);
}
}
}
}
static class DownloadedDrawable extends ColorDrawable {
private final WeakReference<BitmapDownloaderTask> bitmapDownloaderTaskReference;
public DownloadedDrawable(BitmapDownloaderTask bitmapDownloaderTask) {
super(Color.WHITE);
bitmapDownloaderTaskReference = new WeakReference<BitmapDownloaderTask>(
bitmapDownloaderTask);
}
public BitmapDownloaderTask getBitmapDownloaderTask() {
return bitmapDownloaderTaskReference.get();
}
}
// the actual download code
static Bitmap downloadBitmap(String url) {
HttpParams params = new BasicHttpParams();
params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION,
HttpVersion.HTTP_1_1);
HttpClient client = new DefaultHttpClient(params);
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;
}
}
I had this issue and implemented lruCache. I believe you need API 12 and above or use the compatiblity v4 library. lurCache is fast memory, but it also has a budget, so if you're worried about that you can use a diskcache... It's all described in Caching Bitmaps.
I'll now provide my implementation which is a singleton I call from anywhere like this:
//Where the first is a string and the other is a imageview to load.
DownloadImageTask.getInstance().loadBitmap(avatarURL, iv_avatar);
Here's the ideal code to cache and then call the above in getView of an adapter when retrieving the web image:
public class DownloadImageTask {
private LruCache<String, Bitmap> mMemoryCache;
/* Create a singleton class to call this from multiple classes */
private static DownloadImageTask instance = null;
public static DownloadImageTask getInstance() {
if (instance == null) {
instance = new DownloadImageTask();
}
return instance;
}
//Lock the constructor from public instances
private DownloadImageTask() {
// Get max available VM memory, exceeding this amount will throw an
// OutOfMemory exception. Stored in kilobytes as LruCache takes an
// int in its constructor.
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
// Use 1/8th of the available memory for this memory cache.
final int cacheSize = maxMemory / 8;
mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {
#Override
protected int sizeOf(String key, Bitmap bitmap) {
// The cache size will be measured in kilobytes rather than
// number of items.
return bitmap.getByteCount() / 1024;
}
};
}
public void loadBitmap(String avatarURL, ImageView imageView) {
final String imageKey = String.valueOf(avatarURL);
final Bitmap bitmap = getBitmapFromMemCache(imageKey);
if (bitmap != null) {
imageView.setImageBitmap(bitmap);
} else {
imageView.setImageResource(R.drawable.ic_launcher);
new DownloadImageTaskViaWeb(imageView).execute(avatarURL);
}
}
private void addBitmapToMemoryCache(String key, Bitmap bitmap) {
if (getBitmapFromMemCache(key) == null) {
mMemoryCache.put(key, bitmap);
}
}
private Bitmap getBitmapFromMemCache(String key) {
return mMemoryCache.get(key);
}
/* A background process that opens a http stream and decodes a web image. */
class DownloadImageTaskViaWeb extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTaskViaWeb(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon = BitmapFactory.decodeStream(in);
}
catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
addBitmapToMemoryCache(String.valueOf(urldisplay), mIcon);
return mIcon;
}
/* After decoding we update the view on the main UI. */
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
}

Categories

Resources