0


I am writing an app that is essentially a flipcard that shows word/hint on one side and picture on other side relevant to it.I am using viewflipper for the two views.Problem is that the picture loads from internet.App access the db,extracts url and then loads picture.That means the change in view takes as much time as it takes to download the picture.I want to flip card immediately and load picture so that user do not thinks that app is slow.Rather they should know that picture is being loaded,hence the delay.Pls suggest improvement in code.My code for loading picture in flipcard is:

    public void setBMP(String s)  //String passed is url extracted from column of db uing  
    {                             //internal db
   try{
    //String url1 = "c.getString(3)";
    String url1= s;
    System.out.println(url1);
    URL ulrn = new URL(url1);
    HttpURLConnection con = (HttpURLConnection)ulrn.openConnection();
    InputStream is = con.getInputStream();
    Bitmap bmp = BitmapFactory.decodeStream(is);
    if (null != bmp)
    {  
    im.setImageBitmap(bmp);
    }
    else
        System.out.println("The Bitmap is NULL");

    }catch(Exception e){}
    }
        }

For changing view, i have set up actionListener.As soon as user touches screen card flips and image loads.
Also is it possible to preload the images in background while user is viewing some other card.Or is it possible to cache the cards viewed?

Maxsteel
  • 1,922
  • 4
  • 30
  • 55

2 Answers2

1

it seems to me that creating a Runnable that gets the bmp and saves it to a hash map file and then, when it's needed if downloaded, it opens the file, and if not it downloads it from the web.

try looking at fedorvlasov's Lazy adapter for reference.

you can use his Image loader

thepoosh
  • 12,497
  • 15
  • 73
  • 132
1

I would go the asnyctask route, because that way you can load/disable spinners (or wahtever loading animations) as well. Check out this answer for a really simple example. If you want to add spinners you need to start them in the onPreExecute() of the asnyctask (just add it to the example) and disable them in onPostExecute after you image is downloaded.

Using AsyncTask to load Images in ListView

Community
  • 1
  • 1
MikeIsrael
  • 2,871
  • 2
  • 22
  • 34
  • the only problem with using AsyncTask here, is that pre-loading the images can only be done once the activity to show them is loaded. – thepoosh Apr 29 '12 at 09:30
  • @MoeJo I don't understand what the issue would be. That you could only start the asynctask when the activity is started (I thought this was the intent, and it is not true that it cannot be started before)? – MikeIsrael Apr 29 '12 at 10:48
  • 1
    @MikeIsrael, in the AsyncTask, he will need to override the `onPostExecute` and tell the task what to do in the end. if it's just storing the data in files (like I offered in my answer below), it's perfect. BUT, if he wants to control what is viewed on the screen, he Would have to inflate the view in order to avoid a `NullPointerException`. there for, he would have to open the next activity before starting the asyncTask, which is exactly what he was trying to avoid if I understand correctly – thepoosh Apr 29 '12 at 12:45
  • @MikeIsrael Right.Lazy adapter seems to be good option but now what are my options if i want to do it using AsyncTask? – Maxsteel Apr 29 '12 at 20:18
  • @Maxsteel same options as far as I can tell. It is basically like a more advanced version of a thread that has a preparations stage and finishing stage. If you want to save the file to disk you can definitely do that in asynctask. I think it is much better to run it after the activity starts because you don't want to have to wait to load the activity until all your images are loaded. The people at Google who work on Android actually openly looks down on that, and have stated it several times. – MikeIsrael Apr 30 '12 at 06:38