camera captured image not fitting in imageview android

Viewed 126

In my application i am using camera and gallery option to upload image but when image uploaded image is saved with white border.i want to save the image as such without white border.

//Image Scroll
HorizontalScrollView imagescroll = new HorizontalScrollView(this);
    imagescroll.setId(Integer.parseInt(questions.idquestion.get(j) + 700))
LinearLayout.LayoutParams imagelayoutparam = new LinearLayout.LayoutParams(MATCH_PARENT,WRAP_CONTENT);
                            imagescroll.setLayoutParams(imagelayoutparam);

//Image layout
final LinearLayout imagelayout = new LinearLayout(this);
LinearLayout.LayoutParams imlay = new LinearLayout.LayoutParams(MATCH_PARENT, 500);
imlay.setMargins(100, 0, 0, 0);
imagelayout.setLayoutParams(imlay);
imagelayout.setBackgroundColor(Color.RED);
imagelayout.setOrientation(LinearLayout.HORIZONTAL);
imagelayout.setId(Integer.parseInt(questions.idquestion.get(j) + 1000));

//Adding existing images
for (int k = 0; k < images.size(); k++) {
    final ImageView image = new ImageView(this);
    image.setLayoutParams(new android.view.ViewGroup.LayoutParams(500, 500));
    image.setMaxHeight(500);
    image.setMaxWidth(500);
    image.setBackgroundColor(Color.WHITE);
    final int var=k;
    final String abcd;
    String efgh="data:image/jpeg;base64,";
    abcd=efgh.concat(images.get(k));
    String encim = images.get(k).replace("data:image/jpeg;base64,", "");
    Log.d("MMMMM1",abcd);
    byte[] decodedString = Base64.decode(encim, Base64.DEFAULT);
    final Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
    image.setImageBitmap(decodedByte);

this is how image uploaded with white border

3 Answers

Change following code

 final ImageView image = new ImageView(this);
                                image.setLayoutParams(new android.view.ViewGroup.LayoutParams(500, 500));
                                image.setMaxHeight(500);
                                image.setMaxWidth(500);
                                image.setBackgroundColor(Color.WHITE);

to

 final ImageView image = new ImageView(this);
                                image.setLayoutParams(new android.view.ViewGroup.LayoutParams(your int size, your int size));
                                image.setMaxHeight(your int size);
                                image.setMaxWidth(your int size);

If you don't wan't to set specific fixed height than you can set as wrap content

Change this line :

image.setLayoutParams(new android.view.ViewGroup.LayoutParams(500, 500));

to this :

image.setLayoutParams(new android.view.ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 500));

specify property of imageView as scaleType:fitXY and be nice ;)

  final ImageView image = new ImageView(this);
                                        image.setLayoutParams(new android.view.ViewGroup.LayoutParams(500, 500));
                                        image.setMaxHeight(500);
                                        image.setMaxWidth(500);
                                        image.setBackgroundColor(Color.WHITE);
**image.setScaleType(ImageView.ScaleType.FIT_XY);**
Related