因为showDialog()已经过时,我现在必须使用DialogFragments来修复我的Image Picker功能。
我从here获得了代码,到目前为止它就像一个魅力。
现在,为了简单起见,我只会展示必要的Codelines。 首先,点击活动A 上的按钮选择带有此对话框的图像后出现:
ImagePickerFragment ip= new ImagePickerFragment();
ip.show(getSupportFragmentManager(), "imagePicker");
片段代码如下所示:
public class ImagePickerFragment extends DialogFragment{
public Dialog onCreateDialog(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//.. creating an adapter
return new AlertDialog.Builder(getActivity())
.setTitle("select Image")
.setAdapter(adapter, new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which){
if (which == 0){ // from camera
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file = new File(Environment.getExternalStorageDirectory(),
"picture_" + String.valueOf(System.currentTimeMillis()) + ".jpg");
mImageCaptureUri = Uri.fromFile(file);
intent.putExtra(MediaStore.EXTRA_OUTPUT, mImageCaptureUri);
getActivity().startActivityForResult(intent, PICK_FROM_CAMERA);
}
else{ // pick from file
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
getActivity().startActivityForResult(Intent.createChooser(intent, "complete Action..."), PICK_FROM_FILE);
}
}
}).create():
}
}
现在,回到我的活动A 中,onActivityResult()代码如下所示:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_OK) return;
if (requestCode == PICK_FROM_FILE){
mImageCaptureUri = data.getData();
path = getRealPathFromURI(mImageCaptureUri); //from Gallery
if (path == null)
path = mImageCaptureUri.getPath(); //from File Manager
}
else path = data.getData().getPath(); **// this is were the Code starts when picture was taken with camera and where the exceptions come from..**
if(path != null){
// do something with image
}
}
这实际上适用于从画廊拍摄的图像,但出于某种原因不适用于相机拍摄的照片。相机加载后我可以拍照,当我接受该照片进行保存时,应用程序崩溃。
requestCodes是正确的,但我发现从Intent的数据发回的Uri总是为null,即使我把它单独放在intent(即putExtra,bundle)中。 我真的很想知道为什么它适用于galery: - /
也许你们其中一个人知道我做错了什么?
P.S:我很抱歉我的英语不好。更新:我被要求提供崩溃信息。
09-06 01:58:50.929: E/AndroidRuntime(6322): FATAL EXCEPTION: main
09-06 01:58:50.929: E/AndroidRuntime(6322): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=null} to activity {android.pack.coding/android.myactivitys.MainActivity}: java.lang.NullPointerException
09-06 01:58:50.929: E/AndroidRuntime(6322): at android.app.ActivityThread.deliverResults(ActivityThread.java:2980)
09-06 01:58:50.929: E/AndroidRuntime(6322): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3023)
09-06 01:58:50.929: E/AndroidRuntime(6322): at android.app.ActivityThread.access$1100(ActivityThread.java:123)
09-06 01:58:50.929: E/AndroidRuntime(6322): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1177)
09-06 01:58:50.929: E/AndroidRuntime(6322): at android.os.Handler.dispatchMessage(Handler.java:99)
09-06 01:58:50.929: E/AndroidRuntime(6322): at android.os.Looper.loop(Looper.java:137)
09-06 01:58:50.929: E/AndroidRuntime(6322): at android.app.ActivityThread.main(ActivityThread.java:4424)
09-06 01:58:50.929: E/AndroidRuntime(6322): at java.lang.reflect.Method.invokeNative(Native Method)
09-06 01:58:50.929: E/AndroidRuntime(6322): at java.lang.reflect.Method.invoke(Method.java:511)
09-06 01:58:50.929: E/AndroidRuntime(6322): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-06 01:58:50.929: E/AndroidRuntime(6322): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-06 01:58:50.929: E/AndroidRuntime(6322): at dalvik.system.NativeStart.main(Native Method)
09-06 01:58:50.929: E/AndroidRuntime(6322): Caused by: java.lang.NullPointerException
09-06 01:58:50.929: E/AndroidRuntime(6322): at android.myactivitys.MainActivity.onActivityResult(MainActivity.java:298)
09-06 01:58:50.929: E/AndroidRuntime(6322): at android.app.Activity.dispatchActivityResult(Activity.java:4649)
09-06 01:58:50.929: E/AndroidRuntime(6322): at android.app.ActivityThread.deliverResults(ActivityThread.java:2976)
09-06 01:58:50.929: E/AndroidRuntime(6322): ... 11 more