1

In my app after the user has logged in, I have a loading screen and in the background Im doing some preparations. I download the user data and product categories and I place them in a local SQLite base.

How can I set the ImageViews source images for different activities at this stage and not have them load when the activities themselves are created?

For example, Ihave a sliding menu that shows the user image at the too and I want the user image to be already there when the user opens up the sliding menu. I want when the user opens up "My Profile" their data to be already loaded there.

How can I achieve that?

Kaloyan Roussev
  • 14,515
  • 21
  • 98
  • 180

1 Answers1

2

Data will be displayed at the time of activity creation only. But what you can do is load the image bitmap in the background and cache it. And when user opens his sliding menu just set it to the image view instead of loading it again.

Also you can use Universal Image Loader which will load the image only once.

For my profile data you can again fetch all the data from the server while you display loading screen and save this data in SharedPreferences or in application context and when user moves to my profile just set that data to views.

For loading image bitmap you can use following function call it in asynctask:

public static Bitmap loadBitmap(String url) {
    Bitmap bitmap = null;
    InputStream in = null;
    BufferedOutputStream out = null;

    try {
        in = new BufferedInputStream(new URL(url).openStream(), IO_BUFFER_SIZE);

        final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
        out = new BufferedOutputStream(dataStream, IO_BUFFER_SIZE);
        copy(in, out);
        out.flush();

        final byte[] data = dataStream.toByteArray();
        BitmapFactory.Options options = new BitmapFactory.Options();
        //options.inSampleSize = 1;

        bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,options);
    } catch (IOException e) {
        Log.e(TAG, "Could not load Bitmap from: " + url);
    } finally {
        closeStream(in);
        closeStream(out);
    }

    return bitmap;
}

For reference: How to load an ImageView by URL in Android?

Or else I found this post you can try it

URL url = new URL(strUrl);
URLConnection connection = url.openConnection();
connection.setUseCaches(true);
Object response = connection.getContent();
if (response instanceof Bitmap) {
  Bitmap bitmap = (Bitmap)response;
} 

reference: Android image caching

Community
  • 1
  • 1
vipul mittal
  • 17,343
  • 3
  • 41
  • 44
  • can you give me an example for loading the image bitmap in the background and caching it? – Kaloyan Roussev Dec 22 '13 at 08:23
  • what is the difference between saving the data in SharedPreferences like you suggest and storing it in SQLite Database as I was intending to do – Kaloyan Roussev Dec 22 '13 at 08:24
  • 1
    If you have a lot of data in some form of complex structure you should use SQLite but if it's just one user data you can just simply store it in SharedPrefrences. – vipul mittal Dec 22 '13 at 08:27