-2

The issue is causing the app to screen freeze a bit.

I am using loaders to populate the listView which contains imageView and textView.

When the loaders parse the JSON and extract the image URL to object aswell as the title, populating the listView begins.

I did a little of searching inside the code what might cause the problems of skipping frames and i figured out when i populate pictures to listView the lagg comes.

So i'll be posting the code which I use to present pictures to app and if someone can tell me if it can be better and why actually my code is bad?

   ImageView album = (ImageView) listItemView.findViewById(R.id.albumID);
   StrictMode.ThreadPolicy policy = new 
   StrictMode.ThreadPolicy.Builder().permitAll().build();
   StrictMode.setThreadPolicy(policy);

   try {

       Bitmap bitmap = BitmapFactory.decodeStream((InputStream) new URL(currentSong.getImage_url()).getContent());


       album.setImageBitmap(bitmap);

   } catch (MalformedURLException e) {
       e.printStackTrace();
   } catch (IOException e) {
       e.printStackTrace();
   }
Luka Čagalj
  • 31
  • 1
  • 6

1 Answers1

0

Oh god oh god oh god oh god no. Please. No.

You're going against a bunch of Android rules and guidelines.

1) 99.99% of the time, you don't need to modify the ThreadPolicy. So please don't do it unless you know exactly what it does.
2) Never EVER make API calls on the UI thread. This is why the app is freezing.

You need to make an async call, get the image data, and then load it into the ImageView.

There's a lot of libraries who can do this for you, and handle everything smoothly. Try googling "Glide", "Universal Image Loader", "Picasso".

Currently you're locking the thread that is handling UI updates while the image(s) are loading. So while an image is loading, NOTHING can be done with the UI, including scrolling, touching, etc.

Moonbloom
  • 7,738
  • 3
  • 26
  • 38