1

I have String like this

String path = "storage/sdcard0/Pictures/location/img.jpg";

that string is location for file img.jpg. How can i scale/resize that image. thanks..

1 Answers1

0
/*
 * Resizing image size
 */
public static Bitmap decodeFile(String filePath, int WIDTH, int HIGHT) {
    try {

        File f = new File(filePath);

        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(new FileInputStream(f), null, o);

        final int REQUIRED_WIDTH = WIDTH;
        final int REQUIRED_HIGHT = HIGHT;
        int scale = 1;
        while (o.outWidth / scale / 2 >= REQUIRED_WIDTH
                && o.outHeight / scale / 2 >= REQUIRED_HIGHT)
            scale *= 2;

        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    return null;

}

   Bitmap image = decodeFile(_filePaths.get(position), imageWidth, imageHeight);

    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
    imageView.setLayoutParams(new GridView.LayoutParams(imageWidth,
            imageWidth));
   imageView.setImageBitmap(image);
nitish516
  • 174
  • 14
  • still not working, i was try your code like this. String path = "storage/sdcard0/Pictures/location/img.jpg"; decodeFile(path, 400, 200); but file is still same.. – Eko Prasetyo Jun 04 '15 at 12:03
  • Under which layout you are putting this image view? Can you share your complete code. Problem can be somewhere else as well. This code is working fine for me under a grid layout. – nitish516 Jun 04 '15 at 13:17
  • @EkoPrasetyo let us know if your problem is resolved, else share your code. Don't forget to upvote my answer :) – nitish516 Jun 04 '15 at 19:31