J Sol
J Sol

Reputation: 67

Two onActivityResults in one activity

Do you know how to handle two onActivityResult()s in one activity?

I need to use my camera and search for my photos in one activity.

public class MainActivity extends AppCompatActivity {

        public static final int REQUEST_CAPTURE = 1;

        Button button_Vyber_Fotku, button_Fotak;

        ImageView imageView_VyberFotku, imageView_Fotak;
        private static final int PICK_IMAGE = 100;
        Uri imageUri_vybrana;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            imageView_VyberFotku = (ImageView) findViewById(R.id.imageView_VyberFotku);
            button_Vyber_Fotku = (Button) findViewById(R.id.button_Vyber_Fotku);

            imageView_Fotak = (ImageView) findViewById(R.id.imageView_Fotak);
            button_Fotak = (Button) findViewById(R.id.button_fotak);

            if (!hasCamera())
            {
                button_Fotak.setEnabled(false);
            }

        }

           public void Vyber_fotku_clicked(View v)
           {
               openGallery();
           }
        private void openGallery()
        {
            Intent gallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
            startActivityForResult(gallery, PICK_IMAGE);
        }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (resultCode == RESULT_OK && requestCode == PICK_IMAGE){
                imageUri_vybrana = data.getData();
                imageView_VyberFotku.setImageURI(imageUri_vybrana);
            }
        }

        public boolean hasCamera()
        {
            return getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY);
        }

        public void PouzijFotakClicked(View v)
        {
            Intent vyfot = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(vyfot , REQUEST_CAPTURE);
        }


        @Override
        protected void onActivityResult(int requestCode1, int resultCode1, Intent data1)
        {   if (requestCode1 == REQUEST_CAPTURE && resultCode1 == RESULT_OK)
            {
                Bundle extras = data1.getExtras();
                Bitmap photo = (Bitmap) extras.get("data1");
                imageView_Fotak.setImageBitmap(photo);
            }
        }

    }

Upvotes: 0

Views: 1826

Answers (3)

Sachin
Sachin

Reputation: 1448

Check this It works for me

 private static final int CAMERA_ = 999;
 private static final int GALLERY_ = 888;

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // if the result is capturing Image
        if (requestCode == CAMERA_) {
            if (resultCode == RESULT_OK) {

              BitmapFactory.Options options = new BitmapFactory.Options();
                options.inSampleSize = 8;
                UtilsClass.mBitmap = BitmapFactory.decodeFile(fileUri.getPath(),
                        options);
                imageView.setImageBitmap(UtilsClass.mBitmap);

            } else if (resultCode == RESULT_CANCELED) {
                // user cancelled Image capture
                Toast.makeText(getApplicationContext(),
                        "User cancelled image capture", Toast.LENGTH_SHORT)
                        .show();
            } else {
                // failed to capture image
                Toast.makeText(getApplicationContext(),
                        "Sorry! Failed to capture image", Toast.LENGTH_SHORT)
                        .show();
            }

        }else if (requestCode == GALLERY_) {
            if (resultCode == RESULT_OK) {
                Uri selectedImage = data.getData();
                String[] filePath = {MediaStore.Images.Media.DATA};
                Cursor c = getContentResolver().query(selectedImage, filePath, null, null, null);
                c.moveToFirst();
                int columnIndex = c.getColumnIndex(filePath[0]);
                String picturePath = c.getString(columnIndex);
                c.close();
                UtilsClass.mBitmap = (BitmapFactory.decodeFile(picturePath));
                imageView.setImageBitmap(UtilsClass.mBitmap);


            } else if (resultCode == RESULT_CANCELED) {
                // user cancelled Image capture
                Toast.makeText(getApplicationContext(),
                        "User cancelled image capture", Toast.LENGTH_SHORT)
                        .show();
            } else {
                // failed to capture image
                Toast.makeText(getApplicationContext(),
                        "Sorry! Failed to capture image", Toast.LENGTH_SHORT)
                        .show();
            }
        }

Upvotes: 0

Abhishek
Abhishek

Reputation: 16

// define two variable camera and pick_image of int type pass value of request code of desired out put in activity onResult 
@Override
   protected void onActivityResult(int requestCode, int resultCode, Intent data{
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK && requestCode == PICK_IMAGE){
    //first one to pick image 
    //do somthing 
    }else if(resultCode == RESULT_OK && requestCode == Camera){
//use to take image from camera response
}
    }

Upvotes: 0

Kunu
Kunu

Reputation: 5134

Instead of two different method for onActivityResults use single method and distinguish them according to their request code.

@Override
protected void onActivityResult(int requestCode1, int resultCode1, Intent data1){   
    if (requestCode1 == REQUEST_CAPTURE && resultCode1 == RESULT_OK){
            Bundle extras = data1.getExtras();
            Bitmap photo = (Bitmap) extras.get("data1");
            imageView_Fotak.setImageBitmap(photo);
    }
    else if (resultCode1 == RESULT_OK && requestCode1 == PICK_IMAGE){
        imageUri_vybrana = data1.getData();
        imageView_VyberFotku.setImageURI(imageUri_vybrana);
    }
}

Note: You can't have two declaration for single override method.

Upvotes: 3

Related Questions