//根据一些建议,我在我的项目中编写了这段代码,但仍面临同样的问题
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == PICK_IMAGE/PICK_Camera_IMAGE && resultCode == Activity.RESULT_OK)
{
Fragment yourFragment = getSupportFragmentManager().findFragmentById(R.id.frame_container); // same tag while adding fragment for the first time.
if (yourFragment != null)
{
yourFragment.onActivityResult(requestCode, resultCode, data); //calling method that should be defined in your fragment.
}
}
}
//此代码来自MainActivity.java类.. //我应该将以上活动结果代码写入我的Mainfragment类吗?
gallery.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
Intent gintent = new Intent();
gintent.setType("image/*");
gintent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(
gintent, "Select Picture"), PICK_IMAGE);
} catch (Exception e) {
Toast.makeText(mainActivity,
e.getMessage(), Toast.LENGTH_LONG).show();
Log.e(e.getClass().getName(), e.getMessage(), e);
}
d.dismiss();
}
});
camera.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// define the file-name to save photo taken by Camera
// activity
String fileName = "new-photo-name.jpg";
// create parameters for Intent with filename
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, fileName);
values.put(MediaStore.Images.Media.DESCRIPTION,
"Image captured by camera");
// imageUri is the current activity attribute, define
// and save it for later usage (also in
// onSaveInstanceState)
imageUri = getActivity().getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
values);
// create new Intent
Intent intent = new Intent(
MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
startActivityForResult(intent, PICK_Camera_IMAGE);
}
});
// This is the code belongs to my Profile Fragment class..
// I stuck over here didn't get any kind of solution
// I'm using fragment viewpager
答案 0 :(得分:0)
如果这样做,你就错了 ** PICK_IMAGE / PICK_Camera_IMAGE ** 它将划分您的请求代码,结果代码将不同
if(requestCode == PICK_IMAGE/PICK_Camera_IMAGE && resultCode ==Activity.RESULT_OK)
{
//...
}
使用此代码代替旧版
if(requestCode == PICK_IMAGE && resultCode == Activity.RESULT_OK)
{
//...
}else if(requestCode == PICK_Camera_IMAGE && resultCode ==Activity.RESULT_OK)
{
//...
}
答案 1 :(得分:0)
要在片段中获得结果,请务必致电
startActivityForResult(intent, PICK_Camera_IMAGE);
而不是
getActivity().startActivityForResult(intent, PICK_Camera_IMAGE);
在你的片段里面。
答案 2 :(得分:0)
由于所有片段都被调用到一个活动中,因此必须将结果从您正在执行的活动的onActivityResult转移到片段中。
您必须在片段中创建自己的onActivityResult方法,并使用相同的签名来更新片段UI。
答案 3 :(得分:0)
删除此
super.onActivityResult(requestCode, resultCode, data);