我正在制作一个Android应用程序来使用Fragment捕获图像。在星系s2和星系s4上捕获图像。应用程序没有在上述设备上崩溃。当我在Moto G上测试时,应用程序崩溃了。我收到了NullPointerException。过去两天我一直被困在这里。我在下面附加代码和LogCat。 (我已经使应用程序崩溃的行变为粗体)。请帮我。
10-01 11:56:47.545: E/AndroidRuntime(2998): java.lang.RuntimeException: Failure delivering result ResultInfo{who=android:fragment:0, request=1, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.as.billseeker/com.as.billseeker.MainActivity}: java.lang.NullPointerException
10-01 11:56:47.545: E/AndroidRuntime(2998): at android.app.ActivityThread.deliverResults(ActivityThread.java:3432)
10-01 11:56:47.545: E/AndroidRuntime(2998): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3475)
10-01 11:56:47.545: E/AndroidRuntime(2998): at android.app.ActivityThread.access$1300(ActivityThread.java:139)
10-01 11:56:47.545: E/AndroidRuntime(2998): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1258)
10-01 11:56:47.545: E/AndroidRuntime(2998): at android.os.Handler.dispatchMessage(Handler.java:102)
10-01 11:56:47.545: E/AndroidRuntime(2998): at android.os.Looper.loop(Looper.java:136)
10-01 11:56:47.545: E/AndroidRuntime(2998): at android.app.ActivityThread.main(ActivityThread.java:5086)
10-01 11:56:47.545: E/AndroidRuntime(2998): at java.lang.reflect.Method.invokeNative(Native Method)
10-01 11:56:47.545: E/AndroidRuntime(2998): at java.lang.reflect.Method.invoke(Method.java:515)
10-01 11:56:47.545: E/AndroidRuntime(2998): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
10-01 11:56:47.545: E/AndroidRuntime(2998): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
10-01 11:56:47.545: E/AndroidRuntime(2998): at dalvik.system.NativeStart.main(Native Method)
10-01 11:56:47.545: E/AndroidRuntime(2998): Caused by: java.lang.NullPointerException
10-01 11:56:47.545: E/AndroidRuntime(2998): at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1420)
10-01 11:56:47.545: E/AndroidRuntime(2998): at android.content.ContentResolver.query(ContentResolver.java:445)
10-01 11:56:47.545: E/AndroidRuntime(2998): at android.content.ContentResolver.query(ContentResolver.java:404)
10-01 11:56:47.545: E/AndroidRuntime(2998): at com.as.billseeker.BSPlaySuccessActivity.imageFromGallery(BSPlaySuccessActivity.java:198)
10-01 11:56:47.545: E/AndroidRuntime(2998): at com.as.billseeker.BSPlaySuccessActivity.onActivityResult(BSPlaySuccessActivity.java:108)
10-01 11:56:47.545: E/AndroidRuntime(2998): at android.app.Activity.dispatchActivityResult(Activity.java:5450)
10-01 11:56:47.545: E/AndroidRuntime(2998): at android.app.ActivityThread.deliverResults(ActivityThread.java:3428)
10-01 11:56:47.545: E/AndroidRuntime(2998): ... 11 more
代码:
private String imageFromGallery(int resultCode, Intent data) {
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
//selectedImage.getPath();
**Cursor cursor = getActivity().getContentResolver().query(selectedImage,filePathColumn, null, null, null);**
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
return filePath;
}
编辑:回答 我搜索了很多天2天。最后我得到了答案。在moto g,图像路径有问题。我在SD卡上创建了用于存储图像的文件夹。之后我从那条路上拍下了照片。 码: private static File getOutputMediaFile(){
File mediaStorageDir = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
"your_folder_name");
// This location works best if you want the created images to be shared
// between applications and persist after your app has been uninstalled.
// Create the storage directory if it does not exist
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d("instaReport", "failed to create directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
.format(new Date());
File mediaFile;
imagepath = mediaStorageDir.getPath() + File.separator + "IMG_"
+ timeStamp + ".jpg";
mediaFile = new File(imagepath);
return mediaFile;
}
private static Uri getOutputMediaFileUri() {
return Uri.fromFile(getOutputMediaFile());
}
i have write method on button click listener:
private void dispatchTakePictureIntent( int i) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
fileUri = getOutputMediaFileUri();
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(takePictureIntent, i);
}
}
hope this answer helps you.