0

各リスト項目にイメージビューとテキストビューがあるリストビューがあります。galleryを開き、選択した画像を選択して 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);

    }
4

1 に答える 1