Reputation: 477
I have 400 images in my server database. I am able to get those images and display in my emulator. But it is taking lot of time to get the image from server. Hence I want to get the image asynchronously. How can I achieve that task? Help me regarding this...Will be thankful in advance....
I want to convert the following code to asynchronous task....
My Code:
public View getView(.......)
{
ImageView myimgview = (ImageView) view.findViewById(R.id.imageView100);
drawable = LoadImageFromWebOperations(v.getTag().toString());
myimgview.setImageDrawable(drawable);
---
---
---
}
private Drawable LoadImageFromWebOperations(String url) {
try {
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "image.png");
return d;
}
catch (Exception e)
{
System.out.println("Exc=" + e);
return null;
}
}
Upvotes: 1
Views: 1216
Reputation: 4213
Simple solution is use asynchronous task in java
hear some example
private class DownloadImages AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
//use your async task hear
//in your case call LoadImageFromWebOperations(urls[0])
}
@Override
protected void onPostExecute(String result) {
}
}
And call this async task by passing requied image url
DownloadImages task = new DownloadImages ();
task.execute(new String[] { "http://your image url.com" });
Hear is good tutorial for this
http://www.vogella.com/articles/AndroidPerformance/article.html#asynctask
Upvotes: 2
Reputation: 32271
You can use a thread in combination with a handler:
public void setImageInThread(final ImageView imageView, final String url) {
final Handler handler = new Handler() {
@Override
public void handleMessage(Message message) {
Drawable result = (Drawable) message.obj;
imageView.setImageDrawable(result);
}
};
new Thread() {
@Override
public void run() {
Drawable drawable = getDrawable(url);
Message message = handler.obtainMessage(1, drawable);
handler.sendMessage(message);
}
}.start();
}
Where getDrawable is your function to get images synchronously.
Upvotes: 0
Reputation: 8251
I use Prime for all of my image loading, it can take care of this easily.
Upvotes: 0