我有一个片段,我正在调用getActivity().startActivityForResult
进行相机活动,我onActivityResult
中有MainActivity
来处理结果。
片段
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 250);
intent.putExtra("outputY", 250);
try {
intent.putExtra("return-data", true);
getActivity().startActivityForResult(Intent.createChooser(intent,"Complete action using"), PICK_FROM_GALLERY);
} catch (ActivityNotFoundException e) {
// Do nothing for now
}
MainActivity
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_CANCELED) //CANCELED
{
Toast.makeText(this, "canceled", Toast.LENGTH_SHORT).show();
}
switch (requestCode) {
case PICK_FROM_GALLERY:
Toast.makeText(this, "Pick from Gallery", Toast.LENGTH_SHORT).show();
if (resultCode == RESULT_OK) {
Toast.makeText(this, "Result Okay", Toast.LENGTH_SHORT).show();
Bundle extras2 = data.getExtras();
if (extras2 != null) {
//Doesn't enter here
} else {
Toast.makeText(this, "extra is null", Toast.LENGTH_SHORT).show();
}
}
break;
}
}
答案 0 :(得分:1)
活动@Oncreate打开相机意图
// Camera Option Clicked
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePictureIntent, 1);
处理onActivityResult
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 1:
if(resultCode == RESULT_OK){
if (data != null) {
takePhoto(data);
}
}
break;
}
}
在ImageView上显示图像
private void takePhoto(Intent imageData){
Bundle extras = imageData.getExtras();
if(extras != null){
imageView.setImageBitmap((Bitmap) extras.get("data"));
}
}
答案 1 :(得分:0)
更改此行
getActivity().startActivityForResult(Intent.createChooser(intent,"Complete action using"), PICK_FROM_GALLERY);
到
startActivityForResult(Intent.createChooser(intent,"Complete action using"), PICK_FROM_GALLERY);
请参阅此信息以获取更多信息onActivityResult is not being called in Fragment
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_PICK);
startActivityForResult(Intent.createChooser(intent, "Select Picture"),
112);
if (requestCode == 112) {
try {
InputStream inputStream = getContentResolver()
.openInputStream(data.getData());
FileOutputStream fileOutputStream = new FileOutputStream(
mFileTemp);
copyStream(inputStream, fileOutputStream);// do other stuff
fileOutputStream.close();
inputStream.close();
//do other stuff
} catch (Exception e) {
e.printStackTrace();
}
}
答案 2 :(得分:0)
您应该致电startActivityForResult()
而不是getActivity().startActivityForResult()
在Parent Activity类中,覆盖onActivityResult()方法,甚至在Fragment Class中覆盖它,并调用以下代码。
在父类中:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id....);
fragment.onActivityResult(requestCode, resultCode, data);
}
在儿童班:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//in fragment class callback
}
答案 3 :(得分:-1)
你应该打电话
来自你的片段的startActivityForResult(Intent.createChooser(intent,"Complete action using"), PICK_FROM_GALLERY);
,然后在你的Fragment本身中实现onActivityResult(),并在onActivityResult()中检查结果代码为
public void onActivityResult(int requestCode,int resultCode,Intent data){ if(resultCode == Activity.RESULT_OK){ if(requestCode == PICK_FROM_GALLERY){