0

I am having problems with a global variable in my Java code. Here is the thing: My Screen has 3 ImageButtons elements for picking 3 images when pressing on them; this works fine. I am using onActiviyResult in order to make it, but I have implemented just one onActiviyResult method for the 3 images, so I am using 3 if(){...} blocks in the method to know the pressed imagebutton, I mean this:

if(current_picture.equals("pic1"))}{
    imagebutton1.setImageBitmap(bitmap);
}

if(current_picture.equals("pic2"))}{
    imagebutton2.setImageBitmap(bitmap);
}

if(current_picture.equals("pic3"))}{
    imagebutton3.setImageBitmap(bitmap);
}

Here, current_picture is a String, and it's declared outside the onCreate method, and its default value is set to: String current_picture = "";

I use this variable to save a value that was set on the setonclicklistener events of the 3 imagebuttons, I mean:

imagebutton1.setOnClickListener(new View.OnClickListener() {

    @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            current_picture = "pic1";                                                                   
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(intent, "Choose a   
            picture"), SELECT_PICTURE);


        }
    });

Same thing for imagebutton2 (current_picture = "pic2";) and imagebutton3 (current_picture = "pic3";). All these events are on onCreate method obviously.

So, the problem is that current_picture is losing its value set on the setonclicklistener methods when calling onActivityResult method, I mean, current_user value is still "" and not "pic1", "pic2" or "pic3" depending on the pressed imagebutton. I think its value is destroyed when calling the new activity onActivityResult, then onActivityResul just recognizes: String current_picture = "";

I have done many things to solve this but i am able to find a solution, I attached some code (not all, just important parts) below:

public class Publish_Event extends Activity{

    private ImageButton imagebutton1;
    private ImageButton imagebutton2;
    private ImageButton imagebutton3;
    private Bitmap bitmap;
    private String current_picture="";


    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.publicar_eventos);

    StrictMode.ThreadPolicy p = new  
    StrictMode.ThreadPolicy.Builder().permitAll().build();  
    StrictMode.setThreadPolicy(p);

    imagebutton1 = (ImageButton)findViewById(R.id.pic1);
    imagebutton2 = (ImageButton)findViewById(R.id.pic2);
    imagebutton3 = (ImageButton)findViewById(R.id.pic3);


    imagebutton1.setOnClickListener(new View.OnClickListener() {

    @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            current_picture = "pic1";                                                                   
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(intent, "Choose a   
            picture"), SELECT_PICTURE);


        }
    });


    imagebutton2.setOnClickListener(new View.OnClickListener() {

    @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            current_picture = "pic2";                                                                   
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(intent, "Choose a   
            picture"), SELECT_PICTURE);


        }
    });

    imagebutton3.setOnClickListener(new View.OnClickListener() {

    @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            current_picture = "pic3";                                                                   
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(intent, "Choose a   
            picture"), SELECT_PICTURE);


        }
    });


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent  
    data) {
           super.onActivityResult(requestCode, resultCode, data);
           if (resultCode == Activity.RESULT_OK) {
              if (requestCode == SELECT_PICTURE) {
                   Uri selectedImageUri = data.getData();
                   imagen_path = getRealPathFromURI(selectedImageUri);
                   bitmap  = BitmapFactory.decodeFile(imagen_path);




                   if(current_picture.equals("pic1")){

                       imagebutton1.setImageBitmap(bitmap);


                   }

                   if(current_picture.equals("pic2")){

                       imagebutton2.setImageBitmap(bitmap);


                   }

                   if(current_picture.equals("pic3")){

                       imagebutton3.setImageBitmap(bitmap);


                   }



                }
            }
        }

    @TargetApi(Build.VERSION_CODES.KITKAT) 
    public String getRealPathFromURI(Uri contentUri) {


       String[] projection = { MediaStore.Images.Media.DATA };
       Cursor cursor = null;
       try {
           if (Build.VERSION.SDK_INT > 19) {
               // Will return "image:x*"
               String wholeID = DocumentsContract.getDocumentId(contentUri);
               // Split at colon, use second item in the array
               String id = wholeID.split(":")[1];
               // where id is equal to
               String sel = MediaStore.Images.Media._ID + "=?";

               cursor = Publish_Event.this.getContentResolver().query(
                       MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                       projection, sel, new String[] { id }, null);
           } else {
               cursor = 

                   Publish_Event.this.getContentResolver().query(contentUri,
                   projection, null, null, null);
           }
       } catch (Exception e) {
           e.printStackTrace();
       }

       String path = null;
       try {
           int column_index = 
           cursor.getColumnIndex(MediaStore.Images.Media.DATA);
           cursor.moveToFirst();
           path = cursor.getString(column_index).toString();
           cursor.close();
       } catch (NullPointerException e) {
           e.printStackTrace();
       }
       return path;
   }


} 

1 Answers1

1

You can start activity with different request code.

public class Publish_Event extends Activity{
    private static final int SELECT_PICTURE_1 = 1;
    private static final int SELECT_PICTURE_2 = 2;
    private static final int SELECT_PICTURE_3 = 3;

    protected void onCreate(Bundle savedInstanceState) {
        imagebutton1.setOnClickListener(new View.OnClickListener() {

           @Override
           public void onClick(View v) {
               Intent intent = new Intent();
               intent.setType("image/*");
               intent.setAction(Intent.ACTION_GET_CONTENT);
               startActivityForResult(Intent.createChooser(intent, "Choose a picture"), SELECT_PICTURE_1);
           }
        });
        imagebutton2.setOnClickListener(new View.OnClickListener() {

           @Override
           public void onClick(View v) {
               Intent intent = new Intent();
               intent.setType("image/*");
               intent.setAction(Intent.ACTION_GET_CONTENT);
               startActivityForResult(Intent.createChooser(intent, "Choose a picture"), SELECT_PICTURE_2);
           }
        });
   }

   @Override
   protected void onActivityResult(int requestCode, int resultCode, Intent data) {
       super.onActivityResult(requestCode, resultCode, data);
       if (resultCode == Activity.RESULT_OK) {
          if (requestCode == SELECT_PICTURE_1) {
              //change imagebutton1
          }else if(requestCode == SELECT_PICTURE_2){
             //change imagebutton2 
          }else if(requestCode == SELECT_PICTURE_3){
             //change imagebutton3
          }
       }
   }

}

  • It's working but i still have a problem, I have set a default image to imagebutton1, 2 and 3, it's the same image, if I press imagebutton1 and pick the image then it works fine, the new image replaces the default one, but if i press imagebutton2 or imagebutton3 and pick the image then imagebutton1's new image (previously selected) disappears and appears the default one, same thing happens if i press imagebutton2 or imagebutton3 at first. I don't know what is causing this behavior – Jose Ricardo Citerio Alcala Mar 23 '16 at 19:55
  • I am implementing this: Uri selectedImageUri = data.getData(); imagen_path = getRealPathFromURI(selectedImageUri); bitmap = BitmapFactory.decodeFile(imagen_path); after if (resultCode == Activity.RESULT_OK) – Jose Ricardo Citerio Alcala Mar 23 '16 at 19:58
  • May be your image size is to big and OS need extra RAM and kills your activity and when choosing image finished, android will re-create activity. With current_picture also same problem. Try to choose small image. – Berdimurat Masaliev Mar 23 '16 at 20:25
  • if problem with RAM, you can use this https://github.com/coomar2841/image-chooser-library library. We use this in our project, we can upload 3 photo. – Berdimurat Masaliev Mar 23 '16 at 20:39
  • Ok, sometimes it works, imagebutton1, 2 or 3 new image keeps after pressing another imagebutton and picking image – Jose Ricardo Citerio Alcala Mar 23 '16 at 20:55
  • I don't know if this is a ram problem, I think yes, cause my old code with current_picture was working fine on other phones but not on mine – Jose Ricardo Citerio Alcala Mar 23 '16 at 20:56
  • try like this [link](http://stackoverflow.com/questions/19264870/android-choose-image-from-gallery-showing-memory-error) – Berdimurat Masaliev Mar 23 '16 at 21:05
  • 1
    Finally I solved this problem by changing startActivityForResult(Intent.createChooser(intent, "Choose a picture"), SELECT_PICTURE_1); to startActivityForResult(intent,SELECT_PICTURE_1); – Jose Ricardo Citerio Alcala Mar 24 '16 at 18:38
  • I deleted the intent.createChooser, It was consuming a lot of RAM – Jose Ricardo Citerio Alcala Mar 24 '16 at 18:38
  • Is it possible?, Berdimurat – Jose Ricardo Citerio Alcala Mar 24 '16 at 18:39