1

I want to create a new bitmap with a defined size, for example 200x200

Bitmap b = Bitmap.createBitmap(200, 200, Bitmap.Config.ARGB_8888);

Now my understanding is to put inside that bitmap an image of the sd card, but adjusting the image of the SD to the size of the bitmap I created.

Do what I can do with an ImageView using:

<ImageView android:src="@drawable/landscape"
    android:adjustViewBounds="false" />

The result that I look for is the following:

enter image description here

Thanks in advance.

EDIT: post my code

  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 = false;
        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);
    }

--

imageView.setImageBitmap(decodeSampledBitmapFromResource(getResources(), R.drawable.paisaje, 200, 200));
Sergio76
  • 3,835
  • 16
  • 61
  • 88
  • I have tested many variations on this documentation code: https://developer.android.com/training/displaying-bitmaps/load-bitmap.html ...I deleted the part of calculateInSampleSize ... but I can not get the container bitmap to be 200x200px – Sergio76 Nov 21 '16 at 12:45
  • Post your code. – mallaudin Nov 21 '16 at 12:45
  • I added the code to the post – Sergio76 Nov 21 '16 at 12:57
  • I want work with the bitmap, not with an ImageView – Sergio76 Nov 21 '16 at 12:58
  • Yes, after so many tests, in the code I am not defining the size of the bitmap,. But really now, after so much testing, I'm not quite sure where to put it. can you help me? This is the xml of the imageView: – Sergio76 Nov 21 '16 at 13:07

1 Answers1

1

outWidth and outHeight in BitmapFactory.Options do not set the width and height of resulting image. They are only changed when you change inSampleSize and inJustDecodeBounds is set to false, otherwise they are used to read height and width of image.

You can try createScaledBitmap which scales the bitmap and returns a new bitmap.

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

    return Bitmap.createScaledBitmap(BitmapFactory.decodeResource(res, resId), reqWidth, reqHeight, true);
}
mallaudin
  • 4,744
  • 3
  • 36
  • 68
  • Your solution works, but i have a problem, creating a 1000x1000 not adjust the image, like the image above.This is the current result:https://s14.postimg.org/otagxzfe9/Captura_de_pantalla_2016_11_21_a_las_15_19_57.png ..ideas? Thanks – Sergio76 Nov 21 '16 at 14:20
  • what kind of problem? – mallaudin Nov 21 '16 at 14:21
  • Set `android:layout_width` and `android:layout_height` of `ImageView` to `wrap_content` in xml. – mallaudin Nov 21 '16 at 14:26
  • 1
    Finally, I have managed to solve the problem with this link: http://stackoverflow.com/questions/1540272/android-how-to-overlay-a-bitmap-draw-over-a-bitmap .. Anyway, your help I have been very helpful, thank you. – Sergio76 Nov 21 '16 at 15:41