我正在使用相机Intent打开相机:
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
现在我正在尝试获取捕获的照片的路径但是它的投掷错误:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_REQUEST) {
Uri uri = data.getData();
String imagePath = getRealPathFromURI(uri);
}
}
private String getRealPathFromURI(Uri contentUri) {
String[] proj = { MediaStore.Images.Media.DATA };
// import android.support.v4.content.CursorLoader; i import this for CursorLoader
CursorLoader loader = new CursorLoader(getApplicationContext(), contentUri, proj, null, null, null);
Cursor cursor = loader.loadInBackground();
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
它的抛出错误是:
FATAL EXCEPTION: main
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=100, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.xxx.xxx.BbmpActivity}: java.lang.NullPointerException
at android.app.ActivityThread.deliverResults(ActivityThread.java:2536)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:2578)
at android.app.ActivityThread.access$2000(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:965)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.content.ContentResolver.acquireProvider(ContentResolver.java:743)
at android.content.ContentResolver.query(ContentResolver.java:256)
at android.support.v4.content.CursorLoader.loadInBackground(CursorLoader.java:49)
at com.pixel.bbmp.BbmpActivity.getRealPathFromURI2(BbmpActivity.java:546)
at com.pixel.bbmp.BbmpActivity.onActivityResult(BbmpActivity.java:491)
at android.app.Activity.dispatchActivityResult(Activity.java:3908)
at android.app.ActivityThread.deliverResults(ActivityThread.java:2532)
我认为因为这条线引发错误:
CursorLoader loader = new CursorLoader(getApplicationContext(), contentUri, proj, null, null, null);
这里我需要提供的不是getApplicationContext() 我也试过这个,但直到它抛出错误:
CursorLoader loader = new CursorLoader(this, contentUri, proj, null, null, null);
当我启动此应用程序时,它会抛出一个错误:
Could not find class 'android.content.CursorLoader', referenced from method com.xxx.xxx.BbmpActivity.getRealPathFromURI
答案 0 :(得分:1)
而不是像Uri uri = data.getData();
那样写..
if(data!=null){
if (requestCode == CAMERA_REQUEST) {
Uri uri = data.getExtras().get("data");
//or...
Bitmap bitmapPicture = (Bitmap) data.getExtras().get("data");
// other code
}
}
它会正常工作。主要是data.getExtras().get("data");
答案 1 :(得分:0)
CursorLoader loader = new CursorLoader(getApplicationContext(),
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
proj, null, null, null);
答案 2 :(得分:0)
您可以在设置捕获意图之前指定URI:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(this.mCameraFile));
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
mCameraFile是我创建的文件:
this.mCameraFile = File.createTempFile("toto", "jpg", fAlbum);
其中fAlbum是一个代表我的相册文件夹的文件:
this.fAlbum = Environment.getExternalStorageDirectory() + "/dcim/" + "tata";