Andy Vidkjær
Andy Vidkjær

Reputation: 29

Android: Read image as thumb in runtime

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.

Thanks in advance.

/Andy

EDIT: Added some code to look at

Upvotes: 0

Views: 364

Answers (4)

Sunil Chaudhary
Sunil Chaudhary

Reputation: 1247

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

} }

Upvotes: 0

s.d
s.d

Reputation: 29436

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;
}

Upvotes: 0

KOTIOS
KOTIOS

Reputation: 11196

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

Upvotes: 1

Arun C
Arun C

Reputation: 9035

Use

ThumbnailUtils.extractThumbnail

Also consider following this Tutorial

Loading Large Bitmaps Efficiently

Upvotes: 2

Related Questions