【问题标题】:Loading Database Image in List App is Stuck在列表应用程序中加载数据库图像被卡住了
【发布时间】:2018-02-02 09:25:58
【问题描述】:

我已将图像路径保存在数据库中,并将图像保存在文件夹中。在列表视图应用程序中显示图像卡住且缓慢。我不明白如何解决这个问题。

我正在使用以下代码:

strContactImage = customer.getPhotoPath();
Bitmap decodedImage = BitmapFactory.decodeFile(strContactImage);
customerImage.setImageBitmap(decodedImage);

customer.getPhotoPath() 获取文件夹照片路径图片,并正常显示,但是当我添加超过 20 条记录时,应用程序变慢,卡住了。

【问题讨论】:

  • 你需要在后台线程中解码Bitmap
  • 您需要使用 Picasso 或 Glide 或 Universal Image Loader 库以获得更好的缓存和加载。
  • 从path转换成uri并设置成imageview

标签: java android bitmap bitmapfactory


【解决方案1】:

使用 uri 将图像设置为图像视图而不是解码为位图...

strContactImage = customer.getPhotoPath(); 
Uri uri=Uri.fromFile(new File(strContactImage));
// Set the uri into ImageView
customerImage.setImageURI(uri);

也许对你有帮助……

【讨论】:

  • 你把 android:largeHeap="true" 放到应用标签内的清单文件中了吗?
【解决方案2】:

你可以使用任何图片缓存库,这里是毕加索的例子

strContactImage = customer.getPhotoPath();

Picasso.with(context)
   .load(new File(strContactImage))
   .into(customerImage);

【讨论】:

    【解决方案3】:

    检查图像的大小,如果它们占用大量内存,它们会影响内存并使应用程序卡住并变慢!

    解决这个问题

    看这里

    Load a Scaled Down Version into Memory

    并检查方法calculateInSampleSize()和decodeSampledBitmapFromResource()

    it’s not worth loading a 1024x768 pixel image into memory if it will eventually be displayed in a 128x96 pixel thumbnail in an ImageView.

    您可以使用可以帮助您加载图像的库,它们会处理内存使用情况并将图像加载到您的视图中

    图书馆:

    Picasso

    示例:

    Picasso.with(context).load(file).into(imageview);
    

    Glide

    示例:

    GlideApp.with(context).load(file).into(imageview);
    

    工具 - 查看此以了解更多信息:

    TinyPNG - Smart PNG and JPEG compression

    Optimizilla

    【讨论】:

      猜你喜欢
      • 2019-01-26
      • 2019-02-26
      • 1970-01-01
      • 2013-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-07
      • 1970-01-01
      相关资源
      最近更新 更多