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

时间:2015-07-15 09:12:48

标签: android android-intent android-imageview

我有三个使用循环创建的ImageView。我已经在Activity中定义了变量pictureSelected来跟踪单击哪个ImageView。奇怪的是在onActivityResult中,无论我单击哪个ImageView,这个pictureSelected变量值都会重置为0。接下来,我将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);
    }

onActivityResult

@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);
            }
    }
}

1 个答案:

答案 0 :(得分:0)

您可以通过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);
        }