我有一个应用程序,我可以在其中从图库中选择图像,然后返回图像以将其设置在 imageview 上。问题是,当我设置第一张图像,然后再次使用相同的按钮选择下一张图像时,第一张图像消失了,当前图像被设置为第一张图像。我有一个线性布局,我在选择图像时动态创建图像视图。
下面是我的课程,我在按钮上选择图像单击 btnSelectPhotosGallery,然后 onActivityResult 就在那里。
@Override
public void onClick(View v)
{
int id = v.getId();
Intent i;
switch (id)
{
//image selection on this button click
case R.id.btnSelectPhotosGallery:
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_PICK);
startActivityForResult(Intent.createChooser(intent,
"Select Picture"), SELECT_PICTURE);
break;
default:
break;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == RESULT_OK)
{
if (requestCode == SELECT_PICTURE)
{
Uri selectedImageUri = data.getData();
String imagePath = CommonMethods.getPath(ArticleForm2.this, selectedImageUri);
File image = new File(imagePath);
long sizeOfImage = ((image.length()/1024)/1024);
if(sizeOfImage > 1.0)
{
Toast.makeText(ArticleForm2.this, "Image exceeding the Maximum Size(1 MB)", Toast.LENGTH_SHORT).show();
//coverPhotoSelected = false;
}
else
{
//imageCoverPhoto.setImageURI(selectedImageUri);
Bitmap bmp = getPreview(imagePath);
ImageView img = new ImageView(ArticleForm2.this);
img.setLayoutParams(new LayoutParams(300, 300));
img.setImageBitmap(bmp);
linearImages.addView(img);
}
}
}
}
public Bitmap getPreview(String fileName) {
File image = new File(fileName);
BitmapFactory.Options bounds = new BitmapFactory.Options();
bounds.inJustDecodeBounds = true;
BitmapFactory.decodeFile(image.getPath(), bounds);
if ((bounds.outWidth == -1) || (bounds.outHeight == -1)) {
return null;
}
int originalSize = (bounds.outHeight > bounds.outWidth) ? bounds.outHeight
: bounds.outWidth;
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inSampleSize = originalSize / 64;
return BitmapFactory.decodeFile(image.getPath());
}