Reputation: 29
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
Upvotes: 0
Views: 364
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
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
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
Reputation: 9035
Use
ThumbnailUtils.extractThumbnail
Also consider following this Tutorial
Loading Large Bitmaps Efficiently
Upvotes: 2