我有一个列表视图,其中每个列表项都有一些图像视图和一些文本视图。我正在使用意图打开画廊,选择一个选定的图像并将其分配给 imageview 但我的问题是,当我滚动我的列表视图时,当图像设置为该图像视图时,选定的图像消失并且原始图像出现在那地方。那么谁能告诉我为什么会这样,我该如何避免呢?
Intent photoPickerIntent = new Intent(
Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent,
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch (requestCode) {
case SELECT_PHOTO:
if (resultCode == RESULT_OK) {
Uri selectedImage = imageReturnedIntent.getData();
/*
* InputStream imageStream=null; try { imageStream =
* getContentResolver().openInputStream(selectedImage); } catch
* (FileNotFoundException e) { // TODO Auto-generated catch
* block e.printStackTrace(); } Bitmap image =
* BitmapFactory.decodeStream(imageStream);
*/
try {
LinearLayout ll = (LinearLayout) lv.getChildAt(position);
ImageView im = (ImageView) ll
.findViewById(R.id.image);
Bitmap b = decodeUri(selectedImage);
Bitmap circleBitmap = Bitmap.createBitmap(b.getWidth(),
b.getHeight(), Bitmap.Config.ARGB_8888);
BitmapShader shader = new BitmapShader(b, TileMode.CLAMP,
TileMode.CLAMP);
Paint paint = new Paint();
paint.setShader(shader);
Canvas c = new Canvas(circleBitmap);
c.drawCircle(b.getWidth() / 2, b.getHeight() / 2,
b.getWidth() / 2, paint);
im.setImageBitmap(circleBitmap);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} SELECT_PHOTO);
private Bitmap decodeUri(Uri selectedImage) throws FileNotFoundException {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(
getContentResolver().openInputStream(selectedImage), null, o);
// The new size we want to scale to
final int REQUIRED_SIZE = 40;
// Find the correct scale value. It should be the power of 2.
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE) {
break;
}
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeStream(
getContentResolver().openInputStream(selectedImage), null, o2);
}