这是我调用onactivity结果的代码。我正在使用一个对话框,提示从图库或相机中选择图像。相同的代码在活动中工作,但不是碎片。我已经在stackoverflow上尝试了所有以前的答案。请帮忙
AlertDialog.Builder builder = new AlertDialog.Builder(
getActivity());
builder.setTitle("Choose Image Source");
builder.setItems(new CharSequence[] { "Pick from Gallery",
"Take from Camera" },
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
switch (which) {
case 0:
Intent intent = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
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);
startActivityForResult(
Intent.createChooser(
intent,
"Complete action using"),
PICK_FROM_GALLERY);
} catch (ActivityNotFoundException e) {
}
break;
case 1:
Intent takePictureIntent = new Intent(
MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent
.resolveActivity(getActivity()
.getPackageManager()) != null) {
startActivityForResult(
takePictureIntent,
PICK_FROM_CAMERA);
}
break;
default:
break;
}
}
});
builder.show();
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
try {
if (requestCode == PICK_FROM_GALLERY) {
System.out.print("ho ja please");
Bundle extras2 = data.getExtras();
if (extras2 != null) {
bitmap = extras2.getParcelable("data");
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Config.ARGB_8888);
dp.setImageBitmap(output);
}
}
if (requestCode == PICK_FROM_CAMERA) {
Bundle extras = data.getExtras();
Bitmap bitmap1 = (Bitmap) extras.get("data");
bitmap = Bitmap.createScaledBitmap(bitmap1, 250, 250, true);
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Config.ARGB_8888);
dp.setImageBitmap(output);
}
答案 0 :(得分:0)
您可以尝试使用Activity上下文调用startActivityForResult,即。
getActivity().startActivityForResult(Intent.createChooser(
intent,
"Complete action using"),
PICK_FROM_GALLERY);
我使用以下代码显示图像选择器,检查这是否对您有所帮助 -
private static final int SELECT_PICTURE = 1; // Declare this variable
Intent pickIntent = new Intent();
pickIntent.setType("image/*");
pickIntent.setAction(Intent.ACTION_GET_CONTENT);
Intent takePhotoIntent = new Intent(
MediaStore.ACTION_IMAGE_CAPTURE);
String pickTitle = "Select or take picture";
// strings.xml
Intent chooserIntent = Intent.createChooser(pickIntent,
pickTitle);
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
new Intent[] { takePhotoIntent });
getActivity().startActivityForResult(chooserIntent,
SELECT_PICTURE);
然后在您的活动中,您可以使用此 -
protected void onActivityResult(int requestCode, int resultCode,
Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
if (imageReturnedIntent != null) {
if (imageReturnedIntent.getData() != null) {
Uri selectedImage = imageReturnedIntent.getData(); // use this URI for getting and updating the fragment
}
}
}
答案 1 :(得分:0)
这是因为您的警报对话框调用了Activity.startActivityForResult而不是Fragment.startActivityForResult。如果要修复该行为,请使用对话框中的片段引用来调用startActivityForResult。
UPD:同样如上所述@Ashish Tamrakar如果被覆盖,请不要忘记调用Activity.onActivityResult方法中的super.onActivityResult