0

I'm trying to put a image inside an ImageView, but I can't do it. When the method finishes, it doesn't show anything in the ImageView, because loadimage returns null. Why is this?

This is the code:

private void setSign() {
        File[] files = ImagesUtilities.getImages(directory, id);

        for (int i = 0; i < files.length; i++) {
            if (!files[i].getName().contains("firma")) {
                signFile = files[i].getAbsolutePath();
            }
        }
        if(new File(signFile).exists()){
            sign.setImageBitmap(loadImage(signFile));
            sign.invalidate();
        }
    }

    private Bitmap loadImage(String imgPath) {
        BitmapFactory.Options options;
        try {
            options = new BitmapFactory.Options();
            options.inSampleSize = 2;
            Bitmap bitmap = BitmapFactory.decodeFile(imgPath, options);
            return bitmap;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    } 

The value of signFile in the last try its: /storage/emulated/0/Pictures/181/20140414153624/firma357840194.jpg

hichris123
  • 10,145
  • 15
  • 56
  • 70
colymore
  • 11,776
  • 13
  • 48
  • 90

1 Answers1

0
if(new File(signFile).exists()){
        sign.setImageBitmap(BitmapSize.getDecodedBitmap(path, 400, 400););
        sign.invalidate();
    }

Class::::

public class BitmapSize{



public static Bitmap getDecodedBitmap(String path, float target_width, float target_height) {
    Bitmap outBitmap = null;
    try {
        Options decode_options = new Options();
        decode_options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path,decode_options);  //This will just fill the output parameters
        int inSampleSize = calculateInSampleSize(decode_options, target_width, target_height);

        Options outOptions = new Options();
        outOptions.inJustDecodeBounds = false;
        outOptions.inSampleSize = inSampleSize;
        outOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;
        outOptions.inScaled = false;

        Bitmap decodedBitmap = BitmapFactory.decodeFile(path,outOptions);
        outBitmap = Bitmap.createScaledBitmap(decodedBitmap,// (int)target_width, (int)target_height, true);
                (int)((float)decodedBitmap.getWidth() / inSampleSize),
                (int)((float)decodedBitmap.getHeight() / inSampleSize), true);
        System.out.println("Decoded Bitmap: Width "  + outBitmap.getWidth() + " Height = " + outBitmap.getHeight() + " inSampleSize = " + inSampleSize);

    } catch (Exception e) {
        // TODO: handle exception
    }

    return outBitmap;
}

public static int calculateInSampleSize(Options options, float reqWidth, float reqHeight) {
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

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

        final int heightRatio = Math.round((float) height
                / (float) reqHeight);
        final int widthRatio = Math.round((float) width / (float) reqWidth);

        inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
    }

    return inSampleSize;
}

}
Akarsh M
  • 1,629
  • 2
  • 24
  • 47