我想使用一个按钮添加一个图像,然后在一个指定的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();
}
}
现在当我再添加一个缩略图时,还是显示的之前的那个。怎么改正啊?
当然要代替了,因为你没有在布局上添加一个新的视图,只是替换了图片。用一个 LinearLayout代替 RelativeLayout,然后但你什么时候想添加一个新的缩略图时,创建一个新的 ImageView,在 bitmap 上设置 ImageView 的背景,然后添加到 LinearLayout中。 还要记得定义 LinearLayout 的方向。