简体   繁体   中英

How can I set the imageviews and textviews of all my activities at app startup time, not at activity creation

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM