简体   繁体   English

如何处理大量图片。 哪些缩略图会显示在android应用中?

[英]How to handle large amount of pictures. which thumbnails sould be shown in android app?

I have a problem in my android application. 我的Android应用程序有问题。 I use an application which make pictures with the build in camera of a phone/tablet.These pictures are sometimes quite large in size because of the high resolution of the camera. 我使用的应用程序使用手机/平板电脑的内置摄像头拍摄照片。由于摄像头的高分辨率,这些照片有时尺寸很大。

Now i load the application and create a listView, where a small thumbnail of the picture is on the left side and on the right is some text. 现在,我加载该应用程序并创建一个listView,其中图片的小缩略图在左侧,右侧是一些文本。

I create the pictures in this way: 我以这种方式创建图片:

Bitmap a = BitmapFactory.decodeFile(url);
int width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 
         (float) 66.0, context.getResources().getDisplayMetrics());
int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 
         (float) 48.0, context.getResources().getDisplayMetrics());
Bitmap b = Bitmap.createScaledBitmap(a, width, height, false);

The problem is that this step take quite a long time. 问题在于此步骤需要相当长的时间。

Is there any way to make this faster? 有什么办法可以使速度更快?

There is no way to make it faster, but you should not block the UI thread. 没有办法使其更快,但您不应阻塞UI线程。 See 看到

  1. AsyncTask (and many related questions like Using AsyncTask to load Images in ListView ) AsyncTask(以及许多相关问题,例如使用AsyncTask在ListView中加载图像
  2. LruCache LruCache

This is the solution I used taken from here: https://stackoverflow.com/a/5934983/1369222 这是我在这里使用的解决方案: https : //stackoverflow.com/a/5934983/1369222

It works great! 效果很好!

byte[] imageData = null;

        try     
        {

            final int THUMBNAIL_SIZE = 64;

            FileInputStream fis = new FileInputStream(fileName);
            Bitmap imageBitmap = BitmapFactory.decodeStream(fis);

            imageBitmap = Bitmap.createScaledBitmap(imageBitmap, THUMBNAIL_SIZE, THUMBNAIL_SIZE, false);

            ByteArrayOutputStream baos = new ByteArrayOutputStream();  
            imageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
            imageData = baos.toByteArray();

        }
        catch(Exception ex) {

        }

Make sure you do this in an Async or a separate thread task so you don't block up the UI thread! 确保在异步或独立线程任务中执行此操作,以免阻塞UI线程!

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

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