如何知道选择了哪个图像视图

How to know which imageview is selected

我有三个使用循环创建的 ImageView。我已经在 Activity 中定义了变量 pictureSelected 来跟踪单击了哪个 ImageView。奇怪的是在onActivityResult中,这个pictureSelected变量值重置为0,不管我点击哪个ImageView。接下来,我将 pictureSelected 存储到 Intent extra,但结果是一样的。

这是我的代码: (这是来自 onCreate 中调用的函数 initView)

for(int i=0;i<3;i++){
        ImageView temp = new ImageView(this);
        String url = "/images/"+user.getEmail()+"_"+i+".jpg";
        new ImageLoadTask(this,Constants.SERVER_URL+url,temp).execute();
        int width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, getResources().getDisplayMetrics());
        int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, getResources().getDisplayMetrics());
        temp.setLayoutParams(new ViewGroup.LayoutParams(width, height));
        final int finalI = i;
        temp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pictureSelected = finalI;
                Intent intent = new Intent();
                intent.putExtra("selected",pictureSelected);
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                intent.addCategory(Intent.CATEGORY_OPENABLE);
                Log.d("pict select",Integer.toString(pictureSelected));
                startActivityForResult(Intent.createChooser(intent, "Select Picture"), 1);

            }
        });
        LinearLayout linearLayout = (LinearLayout) findViewById(R.id.layoutProfilePictures);
        linearLayout.addView(temp);
        profilePictures.add(temp);
    }

上Activity结果

@Override
protected void onActivityResult(int requestCode, int resultCode,
                                Intent imageReturnedIntent) {
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
    Log.d("selected from result",Integer.toString(imageReturnedIntent.getIntExtra("selected",0)));
    switch(requestCode) {
        case REQ_CODE_PICK_IMAGE:
            if(resultCode == RESULT_OK){
                Uri selectedImage = imageReturnedIntent.getData();
                String[] filePathColumn = {MediaStore.Images.Media.DATA};

                Cursor cursor = getContentResolver().query(
                        selectedImage, filePathColumn, null, null, null);
                cursor.moveToFirst();

                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                String filePath = cursor.getString(columnIndex);
                cursor.close();

                Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
                profilePictures.get(pictureSelected).setImageBitmap(yourSelectedImage);
                user.getPhotoUri()[pictureSelected]=filePath;

                Bitmap bm = BitmapFactory.decodeFile(filePath);
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object
                byte[] b = baos.toByteArray();
                String encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
                String url = user.getEmail() + "_"+pictureSelected+".jpg";
                InstagratApplication.getSocket().emit("upload image",url, encodedImage);
            }
    }
}

您可以通过 setTag()

示例如下:

for(int i=0;i<3;i++){
            ImageView temp = new ImageView(this);
            String url = "/images/"+user.getEmail()+"_"+i+".jpg";
            new ImageLoadTask(this,Constants.SERVER_URL+url,temp).execute();
            int width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, getResources().getDisplayMetrics());
            int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, getResources().getDisplayMetrics());
            temp.setLayoutParams(new ViewGroup.LayoutParams(width, height));
            temp.setTag(url);
            temp.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String selectedPictureUrl = v.getTag();
                    Intent intent = new Intent();
                    intent.putExtra("selected",selectedPictureUrl);
                    intent.setType("image/*");
                    intent.setAction(Intent.ACTION_GET_CONTENT);
                    intent.addCategory(Intent.CATEGORY_OPENABLE);
                    startActivityForResult(Intent.createChooser(intent, "Select Picture"), 1);

                }
            });
            LinearLayout linearLayout = (LinearLayout) findViewById(R.id.layoutProfilePictures);
            linearLayout.addView(temp);
            profilePictures.add(temp);
        }

Log.d是否也打印出0?

在此处的示例代码中,您实际上从未在 onActivityResult 方法中设置 pictureSelected 变量。如果它在您的代码中相同,那就可以解释您的问题。

使用这个

startActivityForResult(Intent.createChooser(intent, "Select Picture"), finalI);

并在 onACtivityResult

if(requestCode==0)
{
    //for first imageview
}
else if(requestCode==1)
{
    //for second imageview
}
else if(requestCode==2)
{
    //for third imageview
}