Executor Service

Put your problem here if it does not fit any of the other categories.

Executor Service

Postby droidarama » Fri Feb 17, 2012 6:27 pm

Hey,

Has anyone had much use with the executor service in android?
I have a class that Implements runnable that gets an image from a url and decodes it into a bitmap, like so:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. IconRunnable implements Runnable{
  2.         String urlString = "";
  3.         Bitmap bitmap = null;
  4.        
  5.         public void run() {
  6.  
  7.                 InputStream is = null;
  8.                 BufferedInputStream bis = null;
  9.                
  10.                 try {
  11.                         URL url = new URL(urlString);
  12.                         URLConnection connection = url.openConnection();
  13.                         connection.connect();
  14.                         is = conn.getInputStream();
  15.                         bis = new BufferedInputStream(is);
  16.                         bmp = BitmapFactory.decodeStream(bis);
  17.                        
  18.                 } catch (IOException e) {
  19.                        
  20.                 } finally {
  21.                         try {
  22.                                 is.close();
  23.                                 bis.close();
  24.                         } catch (IOException e) {
  25.                         }
  26.                 }
  27.                
  28.                
  29.         }
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


My other class IconLoader runs the Executor Service and executes this runnable task inside a method of its own, like so:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class IconLoader {
  2.  
  3.         ExecutorService executor = Executors.newFixedThreadPool(3);
  4.        
  5.         IconRunnable icon = new IconRunnable();
  6.        
  7.         public void execute(){
  8.                
  9.                 executor.execute(icon);
  10.         }
  11. }
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


Can anyone tell me how I can get the stored bitmap from the runnable class back to the main so I can attach it?

Thanks
droidarama
Freshman
Freshman
 
Posts: 9
Joined: Fri Feb 03, 2012 4:48 pm

Top

Re: Executor Service

Postby Phyll » Fri Feb 17, 2012 8:38 pm

Hi droidarama,

I'm not sure if this will work but it seems like you somehow have to move your bitmap out of the runnable. Make it visible to both the activity and the runnable. By the way, it is called bitmap where it is declared and bmp where it is used I think.

My compiler doesn't like everything you have shown there so I can't check that out completely but doing something like that should work.

Phyll
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 20 guests