I am using this following code but it don’t seem to work properly
I am trying to add an image using a button and then showing thumbnail in a specific relativeLayout
public void showViewOfReceiptFromSelecting(String uriString)
{
byte[] imageData = null;
try
{
InputStream fis = this.getContentResolver().openInputStream(Uri.parse((uriString)));
Bitmap imageBitmap = BitmapFactory.decodeStream(fis);
imageBitmap = Bitmap.createScaledBitmap(imageBitmap, 40, 40, false);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
imageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
imageData = baos.toByteArray();
ImageView image = new ImageView(this);
image.setImageBitmap(imageBitmap);
image.setId(counterOfReceipts);
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
rlp.addRule(RelativeLayout.RIGHT_OF, counterOfReceipts - 1);
myRelalativelayout.addView(image, rlp); // a relative Layout i already defined earlier in the code
counterOfReceipts = counterOfReceipts + 1 ;
}
catch(IOException e) {
e.printStackTrace();
}
}
Now the problem is, whenever i try to add one more thumbnail, it replaces the old one. Please tell me what to do …
Best Regards
Of course it gets replaced, because you’re not adding a new view to the layout, just replacing the image in it.
Try replacing the RelativeLayout with a LinearLayout, then whenever you want to add a new thumbnail, create a new ImageView, set that ImageView’s background to your bitmap, then add it to the LinearLayout.
Don’t forget to define your LinearLayout orientation.