简体   繁体   English

显示来自URL的图像,Android应用程序运行缓慢

[英]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. 我试图通过URL在我的应用程序中显示图像,但是(仅当)位图显示时,应用程序太慢了。 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: 因此,我有一个名为ImageDownload的类来进行下载,在我的Fragment中,我有:

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. 然后是ImageBitmap大小和平板电脑RAM大小的问题。 It is taking too much of RAM on loading the ImageBitmap on RAM. 将ImageBitmap加载到RAM上会占用太多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. 您可以通过查看日志来检测到此错误,因为系统面临的频繁页面遗漏比页面击中要频繁,因此System.gc()的调用频率非常高,几乎像是抖动问题。 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. 我确信系统是如此频繁地调用.gc() ,并且System.gc()是在GUI线程中调用的,这会干扰UI体验。

您应该使用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/ http://www.technotalkative.com/android-load-images-from-web-and-caching/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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