0

I was use this code on button onClick for pick image from gallary

I want to pass string from intent using putExtra and from the same activity, i try to get this value from onActiviryResult using getExtra but i am getting null vlaue. Is this any way to getting string value which i passed using intent, and this intent open gallery to pick image then return back to same activiy i want to get that string which i passed in intent.

  Intent intent = new Intent(); 
  intent.setType("image/");
  intent.setAction(Intent.ACTION_PICK);
  intent.putExtra("image_field_tag", field_tag);
  startActivityForResult(Intent.createChooser(intent, "Select Picture"), Integer.parseInt(fieldDetlKey));

from above code intent.putExtra("image_field_tag", field_tag); value i am getting null value in onActivityResult see the below code

@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data){
     super.onActivityResult(requestCode, resultCode, data);
     Bundle bundle = data.getExtras();
     String imgTag = bundle.getString("image_field_tag");
     Log.d("image Tag", imgTag);
     Log.d("requestCode",""+requestCode);
     Log.d("resultCode", ""+resultCode);
}

I am getting the null value of variable imgTag, Please tell me how to get extra value from onActivityResult

Thanx,

Krunal Shah
  • 1,438
  • 1
  • 17
  • 29
  • because you are sending intent to other activity . if you want to receive `image_field_tag` in `onActivityResult` you will need to use `setresult` from Activity which you are starting using `startActivityForResult` – ρяσѕρєя K Jan 18 '13 at 07:19
  • No i want to sending intent to same activity and set image which i pick from the gallery but based on "image_field_tag" value – Krunal Shah Jan 18 '13 at 08:22
  • hey did u get the correct answer for this?? – Exceptional Apr 25 '14 at 06:31
  • Yes, i put my tag as request code, using covert tag into hascode and this hascode pass into request code and compare this has code in onActivityResult – Krunal Shah Apr 25 '14 at 07:29
  • 1
    @KrunalShah It doesn't seem like the efficient solution. Let us know if you figure this out. – atulkhatri Nov 29 '14 at 12:35
  • @atulkhatri I think it's the way. Krunal you may accept Piyush answer – flaviussn Oct 24 '17 at 15:45

2 Answers2

2
/**Hi Use Requestcode for identify every request*/

public static final int SELECT_PICTURE_REQUESTCODE=200;

/**
 * start phone Gallery for image selection.
 */
  private final void GalleryActivity() {
   final Intent mIntent = new Intent();
       mIntent.setType("image/*");
       mIntent.setAction(Intent.ACTION_GET_CONTENT);
       this.startActivityForResult(mIntent, Constant.SELECT_PICTURE_REQUESTCODE);
}

public void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (resultCode == RESULT_OK) {

        if (requestCode == Constant.SELECT_PICTURE_REQUESTCODE  && data != null) {
            final Uri Selected_Image_Uri = data.getData();
            final String Selected_Image_Path = GetImagePath_FromUri(Selected_Image_Uri);
            Toast.makeText(this, "Image Path = " + Selected_Image_Path, Toast.LENGTH_LONG).show();
        }
    }

}

/**This function use for get image file path from uri
  * @param uri for selected image
  * @param context activity reference
  * @return String
  */
private final String GetImagePath_FromUri(Uri uri) {
        final String[] Projection = { MediaStore.Images.Media.DATA };
        final Cursor mCursor = managedQuery(uri, Projection, null, null, null);
        final int column_index = mCursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        mCursor.moveToFirst();
        return mCursor.getString(column_index);
    }
Piyush
  • 5,607
  • 4
  • 27
  • 27
  • 2
    This is not the answer OP is looking. Try to do `putExtra` in the intent and then do `getExtra` in `onActivityResult`, its returning null. – atulkhatri Nov 29 '14 at 12:32
-3

when we want a result to be returned by the child activity, we need to call it by the method startActivityForResult(). When the child activity finishes with the job, it should set the data in an intent and call the method setResult(resultcode, intent) to return the data through the intent.
For Detail .Refer this tutorial

http://saigeethamn.blogspot.in/2009/08/android-developer-tutorial-for_31.html

Priya
  • 1,763
  • 1
  • 12
  • 11
  • I want get value from same activity after pick image from gallery at that time i am getting null value from getExtra which i put when open gallery – Krunal Shah Jan 18 '13 at 07:33