How to know which imageview is selected

Christoper

I have three ImageViews which I created by using a loop. I've already defined the variable pictureSelected in the Activity to track which ImageView is clicked. Strangely in onActivityResult, this pictureSelected variable value is reset to 0, regardless which ImageView I click. Next, I store the pictureSelected to an Intent extra but the result is the same.

Here is my code: (this is from function initView called in onCreate)

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);
            }
    }
}
Kinnar Vasa

You can do this via setTag()

Here is example:

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to know which radio button is selected in jquery?

From Dev

How do I know which value is selected

From Dev

how to know which radio button id selected of which row?

From Java

How can I know which radio button is selected via jQuery?

From Dev

How to know which button the user selected on <paper-dialog>?

From Dev

How to know which text string is selected by user in JavaFX TextArea

From Dev

Tcl/tk How can i know when and which radiobutton is selected?

From Dev

How to know which number option is selected using jquery

From Dev

How to know which button the user selected on <paper-dialog>?

From Dev

how to know which radio button is selected out of all options?

From Dev

How to know which fonts are used in selected part of a PDF document

From Dev

How do I know which object was selected in a select box or is it possible to bind an object to the KnockoutJS view model?

From Dev

How to change images while focusing or focusing so that user know which is selected?

From Dev

How to know the index of radiobutton selected

From Dev

How to know the selected rows in WebGrid

From Dev

How to know what item is selected?

From Dev

How to know the number of selected in option

From Dev

How to let user know, an ImageView is clickable

From Dev

How to let user know, an ImageView is clickable

From Dev

How to know which items are in a QGraphicsItemGroup?

From Dev

How to know which framework is used?

From Dev

How to know which malloc is used?

From Dev

How to know the Site is in which ApplicationPools

From Dev

how to know which device was open

From Dev

How to know which app is bad

From Dev

How to know which commit belongs to which branch

From Dev

How to set selected area of Image to ImageView

From Dev

WPF. RadGridView. How can I know which column is selected(sorting)? CurrentColumn binding doesn't work

From Dev

Trying to know which element is currently selected on a @Html.DropDownListFor

Related Related

  1. 1

    How to know which radio button is selected in jquery?

  2. 2

    How do I know which value is selected

  3. 3

    how to know which radio button id selected of which row?

  4. 4

    How can I know which radio button is selected via jQuery?

  5. 5

    How to know which button the user selected on <paper-dialog>?

  6. 6

    How to know which text string is selected by user in JavaFX TextArea

  7. 7

    Tcl/tk How can i know when and which radiobutton is selected?

  8. 8

    How to know which number option is selected using jquery

  9. 9

    How to know which button the user selected on <paper-dialog>?

  10. 10

    how to know which radio button is selected out of all options?

  11. 11

    How to know which fonts are used in selected part of a PDF document

  12. 12

    How do I know which object was selected in a select box or is it possible to bind an object to the KnockoutJS view model?

  13. 13

    How to change images while focusing or focusing so that user know which is selected?

  14. 14

    How to know the index of radiobutton selected

  15. 15

    How to know the selected rows in WebGrid

  16. 16

    How to know what item is selected?

  17. 17

    How to know the number of selected in option

  18. 18

    How to let user know, an ImageView is clickable

  19. 19

    How to let user know, an ImageView is clickable

  20. 20

    How to know which items are in a QGraphicsItemGroup?

  21. 21

    How to know which framework is used?

  22. 22

    How to know which malloc is used?

  23. 23

    How to know the Site is in which ApplicationPools

  24. 24

    how to know which device was open

  25. 25

    How to know which app is bad

  26. 26

    How to know which commit belongs to which branch

  27. 27

    How to set selected area of Image to ImageView

  28. 28

    WPF. RadGridView. How can I know which column is selected(sorting)? CurrentColumn binding doesn't work

  29. 29

    Trying to know which element is currently selected on a @Html.DropDownListFor

HotTag

Archive