Search code examples
androidimageviewbundleandroid-tabactivity

Does child activity of TabActivity supports onActivityResult() method?


I know there are lots of question related to my question, but i dint get any answer who will solve my problem, Basically my requirement is choose the image from gallary and set that image back to ImageView of child activity of Tab Activity, but in TabActivity i unable to get call to the onActivityResult() method, Since yesterday i was trying to search another way to solve the issue, as i found onActivityResult() will not be going to work i tried to pass image using bundle but i was getting !!! FAILED BINDER TRANSACTION !!! error, how do i handle above situations, please suggest me way to call onActivityResult() method into child activity of TabActivity, Thanks in advance.

My Code is

  public void openGallary(int req_code) {

    Intent i = new Intent(Intent.ACTION_PICK,
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

    startActivityForResult(i, req_code);

}

Here is my onActivityResult() method in which i passed requestcode from openGallary() method:

    @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK
            && data != null) {

        Uri pickedImage = data.getData();
        String[] filePath = { MediaStore.Images.Media.DATA };
        Cursor cursor = getContentResolver().query(pickedImage, filePath,
                null, null, null);
        cursor.moveToFirst();
        String imagePath = cursor.getString(cursor
                .getColumnIndex(filePath[0]));

        imgShowLocationImage.setImageBitmap(BitmapFactory
                .decodeFile(imagePath));
        cursor.close();
    }}

Solution

  • Make this on button pick this will take you to gallery where you will be able to pick image.

                    Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);                           
                startActivityForResult(i, LOAD_IMAGE_RESULTS);
    

    And then you call onActivityResult like this .

         if (requestCode == LOAD_IMAGE_RESULTS && resultCode == RESULT_OK && data != null) 
            {
    
                Uri pickedImage = data.getData();
                String[] filePath = { MediaStore.Images.Media.DATA };         
              Cursor cursor = getContentResolver().query(pickedImage, filePath, null, null, null);
                cursor.moveToFirst();
               String imagePath = cursor.getString(cursor.getColumnIndex(filePath[0]));                        
               // image.setImageBitmap(BitmapFactory.decodeFile(imagePath));      
    
                image.setImageBitmap(BitmapFactory.decodeFile(imagePath));
               cursor.close();
           }
    

    This way picked image will apper in your ImageView.