How to use multi thread to download image and render them

Problems with Canvas, OpenGL, etc...

How to use multi thread to download image and render them

Postby emgainho » Tue Nov 16, 2010 1:38 pm

Hi all,

I read a tutorial to draw 2D graphics in http://www.droidnova.com/playing-with-g ... mment-1454

I run it successfully when I render many images that I download from the web.

But application pause when they are downloading.

After the downloading is finished, application will draw them on canvas.

Now, I want to use multi-thread to download and display image (example 1 main thread for displaying (in that tutorial code) and and 1 thread for downloading) with available code in that tutorial.

In this case, canvas will be updated continuously.

Please help me.

Thanks,
emgainho
Junior Developer
Junior Developer
 
Posts: 20
Joined: Thu Aug 19, 2010 1:45 pm

Top

Re: How to use multi thread to download image and render the

Postby emgainho » Thu Nov 18, 2010 9:04 am

anyone can help me?
emgainho
Junior Developer
Junior Developer
 
Posts: 20
Joined: Thu Aug 19, 2010 1:45 pm

Re: How to use multi thread to download image and render the

Postby Schermvlieger » Thu Nov 18, 2010 9:27 am

Hi emgainho,

Try this:

Code: Select all
final Handler downloadReadyHandler = new Handler();
Thread downloadThread;
final Runnable downloadComplete = new Runnable() {
public void run() {
    <put code here to let your main thread know the picture has been downloaded and can be displayed>
    }
};
downloadThread = new Thread() {
    public void run() {
         <put your downloading code here>
         downloadReadyHandler.post(downloadComplete);
    }
};
downloadThread.start();


Good luck!
Schermvlieger
Senior Developer
Senior Developer
 
Posts: 159
Joined: Fri Feb 26, 2010 1:37 pm

Re: How to use multi thread to download image and render the

Postby emgainho » Fri Nov 19, 2010 3:07 am

Schermvlieger wrote:Hi emgainho,

Try this:

Code: Select all
final Handler downloadReadyHandler = new Handler();
Thread downloadThread;
final Runnable downloadComplete = new Runnable() {
public void run() {
    <put code here to let your main thread know the picture has been downloaded and can be displayed>
    }
};
downloadThread = new Thread() {
    public void run() {
         <put your downloading code here>
         downloadReadyHandler.post(downloadComplete);
    }
};
downloadThread.start();


Good luck!


-> thank you a lot

and how can I "let your main thread know the picture has been downloaded and can be displayed" as you said?
emgainho
Junior Developer
Junior Developer
 
Posts: 20
Joined: Thu Aug 19, 2010 1:45 pm

Re: How to use multi thread to download image and render the

Postby Schermvlieger » Fri Nov 19, 2010 9:14 am

Maybe you will not need it, but just remember that in that run method you can do whatever you want to make sure that the "drawing" thread is not trying to display a bitmap that has not been fully downloaded yet. I cannot be more specific since I don't know the structure of your program; you will find out if and how you can use it in due time.
Schermvlieger
Senior Developer
Senior Developer
 
Posts: 159
Joined: Fri Feb 26, 2010 1:37 pm

Re: How to use multi thread to download image and render the

Postby emgainho » Mon Nov 22, 2010 8:46 am

Schermvlieger wrote:Maybe you will not need it, but just remember that in that run method you can do whatever you want to make sure that the "drawing" thread is not trying to display a bitmap that has not been fully downloaded yet. I cannot be more specific since I don't know the structure of your program; you will find out if and how you can use it in due time.


-> thank you very much.

My structure app is the same as this link : http://www.droidnova.com/playing-with-g ... mment-1454

can you give me some suggestion?
emgainho
Junior Developer
Junior Developer
 
Posts: 20
Joined: Thu Aug 19, 2010 1:45 pm

Top

Re: How to use multi thread to download image and render the

Postby Schermvlieger » Mon Nov 22, 2010 9:02 am

I use this same template for most of my applications that display graphics. I think it is a sound piece of code and you won't find many issues with it when you use it as a basis for your app. Just try to find a way to save the downloaded image locally and decode it with bitmapfactory from there in your separate thread, and your problem is gone.
Schermvlieger
Senior Developer
Senior Developer
 
Posts: 159
Joined: Fri Feb 26, 2010 1:37 pm

Re: How to use multi thread to download image and render the

Postby emgainho » Wed Nov 24, 2010 3:51 am

Schermvlieger wrote:I use this same template for most of my applications that display graphics. I think it is a sound piece of code and you won't find many issues with it when you use it as a basis for your app. Just try to find a way to save the downloaded image locally and decode it with bitmapfactory from there in your separate thread, and your problem is gone.


-> Thank you.

But this is the 1st time I get acquainted with multi-thread programming.
can you give me an example?
emgainho
Junior Developer
Junior Developer
 
Posts: 20
Joined: Thu Aug 19, 2010 1:45 pm

Top

Return to Android 2D/3D Graphics - OpenGL Problems

Who is online

Users browsing this forum: No registered users and 1 guest