user1282637
user1282637

Reputation: 1867

Bitmap is taking long to load and display in Android app

right now I have a LocationAdapter class that takes in my "Location" object and displays each Location into a ListView. I am trying to pass it in a Bitmap image and have it display that image in the ListView row. It works, BUT the image takes a while to load, even after the text in the row is loaded. I am testing on an actual device. Here is my Activity code:

public class MainActivity extends ActionBarActivity {

ArrayList<Location> arrayOfLocations;
LocationAdapter adapter;
private static Bitmap bitmap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_main);

    // Construct the data source
    arrayOfLocations = new ArrayList<Location>();

    // Create the adapter to convert the array to views
    adapter = new LocationAdapter(this, arrayOfLocations);

    Bitmap image = getBitmapFromID(1);

    adapter.add(new Location(image, "Place 1",
            "We have the freshest fruit in the whole world!", "2 miles",
            "8-5 mon-sat\nclosed sun"));
    adapter.add(new Location(image, "Place 2",
            "We have the freshest fruit in the whole world!", "2 miles",
            "8-5 mon-sat"));
    adapter.add(new Location(image, "Place 3",
            "We have the best cakes in the whole world!", "2 miles",
            "8-5 mon-fri\n10-1 sat\nclosed sun\nclosed Memorial Day"));
    adapter.add(new Location(image, "Fruit Stand",
            "Best place!", "2 miles",
            "8-5 mon-sat"));
    adapter.add(new Location(image, "Place 4",
            "Food!", "2 miles",
            "8-5 mon-sat"));
    adapter.add(new Location(image, "Place 5",
            "Toys!", "2 miles",
            "8-5 mon-sat"));

    // Attach the adapter to a ListView
    ListView listView = (ListView) findViewById(R.id.listView1);

    View header = (View) getLayoutInflater().inflate(
            R.layout.listview_header, null);
    listView.addHeaderView(header);

    listView.setAdapter(adapter);

}

public static Bitmap getBitmapFromID(int id) {

    //final Bitmap bitmap;

    Thread thread = new Thread(new Runnable() {
        @Override
        public void run() {

            try {
                bitmap = BitmapFactory
                        .decodeStream((InputStream) new URL(
                                "http://icons.iconarchive.com/icons/yellowicon/game-stars/256/Mario-icon.png")
                                .getContent());

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    });

    thread.start();
    return bitmap;
}

}

Is there anything I do doing wrong? Is it normal for this to take a while to load? Thanks!

Upvotes: 0

Views: 1191

Answers (3)

Martin Konecny
Martin Konecny

Reputation: 59571

You need to AsyncTask your work in a background thread. However for even better approach, take a look here:

https://stackoverflow.com/a/23941258/276949

Android Volley is a fairly new non-verbose library from Google that works extremely well for loading images, and takes care of handling all downloads on separate threads automatically.

Upvotes: 1

MJ93
MJ93

Reputation: 5286

Can you have the Mario_icon.png placed INSIDE of your app instead of being accessed from a URL? That way, the app will take less time retrieving the image and it will allow the app to require less permissions

It takes much longer to load an image when you need to connect to a URL.

Upvotes: 0

greywolf82
greywolf82

Reputation: 22173

The loading of the bitmap can take a while as you can read here: Android doc. However you need to sync your thread because after you call start() the method return immediately. You need to receive a "hey I'm done" from your thread, and then load the bitmap.

Upvotes: 0

Related Questions