我在按钮onClick上使用此代码从gallary中选择图像
我想使用putExtra和相同的活动从intent传递字符串,我尝试使用getExtra从onActiviryResult获取此值,但我得到null vlaue。这是获取我使用意图传递的字符串值的任何方式,并且这个意图打开画廊来选择图像然后返回到相同的活动我想要获得我传递意图的字符串。
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的null值,请告诉我如何从onActivityResult获取额外的值
Thanx,
答案 0 :(得分: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);
}
答案 1 :(得分:-3)
当我们想要子活动返回结果时,我们需要通过startActivityForResult()方法调用它。当子活动完成作业时,它应该在intent中设置数据并调用方法setResult(resultcode,intent)以通过intent返回数据。
详情请参阅本教程
http://saigeethamn.blogspot.in/2009/08/android-developer-tutorial-for_31.html