简体   繁体   中英

Show image from URL, Android-app goes extremly slow

I am trying to show an image in my app from an URL, but when (only when) the bitmap is showed the app gets way too slow. The app goes OK, but when I load the image it goes extremly slow. Someone can help with this?

So I have a class named ImageDownload which does the download and in my Fragment I have:

new ImageDownload((ImageView) login_image, this.getActivity().getApplicationContext(),"login_image")
        .execute(path);

Here is my download Class:

public class ImageDownload extends AsyncTask<String, Void, Bitmap> {
private Context mContext;
private String filename;
ImageView bmImage;

public ImageDownload(ImageView bmImage, Context mContext, String filename) {
    this.bmImage = bmImage;
    this.mContext = mContext;
    this.filename = filename;
}

protected Bitmap doInBackground(String... urls) {
    String urldisplay = urls[0];
    Bitmap mIcon11 = null;
    try {
        InputStream in = new java.net.URL(urldisplay).openStream();

        mIcon11 = BitmapFactory.decodeStream(in,null,null);
        //EDIT:

        in.close();

        // The isuue is not soved yet 

    } catch (Exception e) {

        e.printStackTrace();

    }
    return mIcon11;
}

protected void onPostExecute(Bitmap result) {

    bmImage.setImageBitmap(result);

}

}

Then the problem with the size of your ImageBitmap & the RAM size of the tablet. It is taking too much of RAM on loading the ImageBitmap on RAM. You can detect this by looking into the log, how frequent System.gc() is getting called, as the system facing too frequent page misss than page hit, its almost like thrashing problem. I'm sure the system is calling .gc() so frequently and as the System.gc() is called in GUI thread, it is disturbing UI experience.

您应该使用Google的Volley库

Use Picasso . It's simple and cool.

I finally found the response. Check it out. it might help you in the future. Thank you to all of you. This works perfectly:

http://www.technotalkative.com/android-load-images-from-web-and-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