0

我在按钮 onClick 上使用此代码从画廊中挑选图像

我想使用 putExtra 从意图传递字符串,并且从同一活动中,我尝试使用 getExtra 从 onActiviryResult 获取此值,但我得到空值。这是获取我使用意图传递的字符串值的任何方式,并且此意图打开画廊以选择图像然后返回到相同的活动,我想获取我在意图中传递的字符串。

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

从上面的代码intent.putExtra("image_field_tag", field_tag);值,我在onActivityResult中得到空值,请参见下面的代码

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

我得到了变量 imgTag 的空值,请告诉我如何从 onActivityResult 中获取额外的值

谢谢,

4

2 回答 2

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

公共静态最终 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);
    }
于 2013-01-18T08:46:25.603 回答
-3

当我们希望子活动返回结果时,我们需要通过 startActivityForResult() 方法调用它。当子活动完成作业时,它应该在意图中设置数据并调用方法 setResult(resultcode, intent) 通过意图返回数据。
有关详细信息。请参阅本教程

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

于 2013-01-18T07:25:26.363 回答