-5

I am trying to implement one code where user will add image and the size of image should be less then 3MB if the size is greater then it will resize automatically. I know this question is repeated asking but my code is not working properly.

Below is my code

  Uri selectedImage = data.getData();
            String[] filePath = {MediaStore.Images.Media.DATA};
            Cursor c = getContentResolver().query(selectedImage, filePath, null, null, null);
            c.moveToFirst();
            int columnIndex = c.getColumnIndex(filePath[0]);
            String picturePath = c.getString(columnIndex);
            c.close();
            bitmap2 = (BitmapFactory.decodeFile(picturePath));
            Log.e("path of image 4", picturePath + "");
            String str = String.valueOf(bitmap2.getRowBytes() * bitmap2.getHeight());
            Log.e("lengh of bitmap.......", str);
          //  Log.e("lengh of bitmap", String.valueOf(bitmap2.getAllocationByteCount()));

            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            bitmap2.compress(Bitmap.CompressFormat.JPEG, 100, bos);
            byte[] bitmapdata = bos.toByteArray();

            Bitmap resized = Bitmap.createScaledBitmap(bitmap2, 200, 300, true);

            imageView2.setImageBitmap(resized);
            setImage("image2");

I have tried this link

And this

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
pb007
  • 153
  • 1
  • 5
  • 13
  • duplicate question: https://stackoverflow.com/questions/10413659/how-to-resize-image-in-android – Tenten Ponce Dec 27 '17 at 06:36
  • 3
    Possible duplicate of [How to resize Image in Android?](https://stackoverflow.com/questions/10413659/how-to-resize-image-in-android) – ADM Dec 27 '17 at 06:37
  • @T I know this is duplicate question that's why I wrote on question " this question is repeated" please check question – pb007 Dec 27 '17 at 06:39
  • @TentenPonce and ADM please check question. And I have already tried this which link you both send me. I hope you will read my question again thanku. – pb007 Dec 27 '17 at 06:53
  • @user6734679 "my code is not working properly" is not sufficient as a problem description. You need to tell us *exactly* what's happening that you don't expect *and* what you are expecting to happen, along with providing a [MCVE] which duplicates the problem (including any needed input data). People have pointed out a duplicate, and you have mentioned it in the question. It is incumbent upon *you* to [edit] the question to demonstrate that it's not a duplicate (e.g. show *exactly* what you tried and explain what happened (i.e. *exactly* what is "not working properly")). – Makyen Dec 27 '17 at 20:05
  • Okay thanku @Makyen – pb007 Jan 02 '18 at 06:47

1 Answers1

4

HI as per my understanding, You can do this and also you can follow This Link

Bitmap yourBitmap;
Bitmap resized = Bitmap.createScaledBitmap(yourBitmap, newWidth, newHeight, true);

or:

resized = Bitmap.createScaledBitmap(yourBitmap,(int)(yourBitmap.getWidth()*0.8), (int)(yourBitmap.getHeight()*0.8), true);

Also You can use this method

public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {

int width = bm.getWidth();
int height = bm.getHeight();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// create a matrix for the manipulation
Matrix matrix = new Matrix();
// resize the bit map
matrix.postScale(scaleWidth, scaleHeight);
// recreate the new Bitmap
Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);

return resizedBitmap;

 }
coder_baba
  • 447
  • 3
  • 21
  • I have tried this but not working please check my code again – pb007 Dec 27 '17 at 06:40
  • I have updated my answer.You can use method as I my answer – coder_baba Dec 27 '17 at 06:42
  • Can you tell me exact that when I am using above code it's not working – pb007 Dec 27 '17 at 07:00
  • It still gives same size after and before save the image. – pb007 Dec 27 '17 at 07:04
  • 1
    *Please*, indent your code and use one [indenting style](https://en.wikipedia.org/wiki/Indent_style) consistently throughout your code. Doing so makes it **much** easier to read/maintain. Doing so for code you place on Stack Overflow makes it much more likely both that users will up-vote your posts and that people will put time into Answering your Questions. It doesn't really matter which style your choose (although, for some languages, some styles are more appropriate than others). But, *pick one* and *use it consistently* for all code in a single project. – Makyen Dec 27 '17 at 19:48