1

My requirement is what ever the size(width,height not file size) image stored in the database i need to retrieving that as simple passport size of image.

My code is :

byte[] imgByte=null; 

in the oncreate method

imageview=new ImageView(this);
imageview.setLayoutParams(llp2);
imgByte=cursor.getBlob(cursor.getColumnIndex("imagestore"));
imageview.setScaleType(ScaleType.CENTER);
imageview.setImageBitmap(BitmapFactory.decodeByteArray(imgByte, 0, imgByte.length));
layout.addView(imageview)

when i displaying that displayed only what ever the size before enter. so i need to fit that image size in the android.

I tried these links

Reduce size of Bitmap to some specified pixel in Android

but the problem here is i got image in byte. But all codes image data type int only. can i get the correct solution to decrease the image size pragmatically?

Community
  • 1
  • 1
Shankar
  • 269
  • 4
  • 17

1 Answers1

0

You can use following code to create sample sized image. This will return Bitmap.

public static Bitmap decodeSampledBitmapFromResource(Resources res,
        int resId, int reqWidth, int reqHeight) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(res, resId, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth,
            reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeResource(res, resId, options);
} 

public static int calculateInSampleSize(BitmapFactory.Options options,
        int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {

        // Calculate ratios of height and width to requested height and
        // width
        final int heightRatio = Math.round((float) height
                / (float) reqHeight);
        final int widthRatio = Math.round((float) width / (float) reqWidth);

        // Choose the smallest ratio as inSampleSize value, this will
        // guarantee
        // a final image with both dimensions larger than or equal to the
        // requested height and width.
        inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
    }

    return inSampleSize;
}

Use this code,

decodeSampledBitmapFromResource(getResources(),R.drawable.xyz, 100, 100);

Here, 100 * 100 sample size you are providing. So Thumbnail of such size will be created.

Edit

To use bytes[] in code, use following short of code.

public static Bitmap decodeSampledBitmap(byte[] data, int reqWidth,
        int reqHeight) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeByteArray(data, 0, data.length, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth,
            reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeByteArray(data, 0, data.length, options);
}
Chintan Rathod
  • 25,864
  • 13
  • 83
  • 93
  • Ur code is fine.. But I already did. Here the thing is i didn't get image from drawable. I get from the database. and that image i converted into bytes. decodeSampledBitmapFromResource(getResources(),imgByte, 100, 100); On that way i wrote so that total line shows error. The method decodeSampledBitmapFromResource(Resources, int, int, int) in the type Display is not applicable for the arguments (Resources, byte[], int, int) so what can i do? – Shankar Jul 10 '13 at 09:42
  • SO you want to scale bytes? – Chintan Rathod Jul 10 '13 at 09:43
  • yes.. see my code above imgByte=cursor.getBlob(cursor.getColumnIndex("imagestore")); that is the line i used to get database image. can u please help me ? – Shankar Jul 10 '13 at 09:45
  • Thanks alot Chintan Rathod.. Here i face again some problem .. If i set the image size 100 , 100 i will working fine. If i store the small image into database. I updated my code to 300,300 the image size still same. it will not comes increase size image. If i uploaded different size of images in the retrieving side all same width and height i want? please help me. – Shankar Jul 10 '13 at 10:01