Android: Read image as thumb in runtime

时光毁灭记忆、已成空白 提交于 2019-12-10 12:08:39

问题


For a custom gallery im creating using a gridview with imageviews, i wish to read an image from sd storage. This gives me huge perfomance issues, because it will read the whole image, and load this into the imageview.

  • How can i read a large image as a thumb in runtime with emphasis on performance?

    
    
    

    File imgFile = new File(img.getInternalImagePath()); if(imgFile.exists()){ Bitmap myBitmap; try { BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = 4; myBitmap = BitmapFactory.decodeStream(new FlushedInputStream(new FileInputStream(imgFile)),null,options); picture.setImageBitmap(myBitmap);

Thanks in advance.

/Andy

EDIT: Added some code to look at


回答1:


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();

OR

Bitmap thumbBitmap = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(filePath), thumbWidth, thumbHeight);



回答2:


Use

ThumbnailUtils.extractThumbnail

Also consider following this Tutorial

Loading Large Bitmaps Efficiently




回答3:


Popular trick is to sample the original image by skipping some pixels. Somewhat optimized code (square thumbnail only):

public static Bitmap getImageThumbnail(String filePath, int size) {

    Options queryOPs = queryBitmap(filePath);

    int imgSize = Math.max(queryOPs.outWidth, queryOPs.outHeight);
    imgSize = Math.max(imgSize, size);

    int sampleSize = 1;

    while (imgSize / (sampleSize * 2) > size) {
        sampleSize *= 2;
    }

    Options decodeOps = new Options();
    decodeOps.inSampleSize = sampleSize;

    Bitmap img = BitmapFactory.decodeFile(filePath, decodeOps);

    if (img == null) {
        return null;
    }

    Bitmap thumb = Bitmap.createBitmap(size, size, Bitmap.Config.RGB_565);
    Canvas can = new Canvas(thumb);

    Paint pnt = new Paint(Paint.DITHER_FLAG);

    can.drawBitmap(img, 0, 0, pnt);
    return thumb;
}

private static Options queryBitmap(String filePath) {

    Options ops = new Options();
    ops.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(filePath, ops);
    return ops;
}



回答4:


@Override
public View getView(final int position, View view, ViewGroup parent) {
    pos = position;
    View v = view;
    RecordHolder holder = null;
    LayoutInflater mInflater = (LayoutInflater) mContext
            .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

    if (v == null) {
        v = mInflater.inflate(R.layout.items, parent, false);
        holder = new RecordHolder();
        holder.imagev = (ImageView) v.findViewById(R.id.imag_v);
        v.setTag(holder);
    } else {

        holder = (RecordHolder) v.getTag();
    }
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 6;
    Bitmap bitmap1 = BitmapFactory.decodeResource(v.getResources(),
            items.get(position), options);
    holder.imagev.setImageBitmap(bitmap1);

} }



来源:https://stackoverflow.com/questions/18015687/android-read-image-as-thumb-in-runtime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!