尝试创建文件文件时的Nullpointerexception =新文件(路径)

时间:2014-05-09 15:47:36

标签: android file

我有一个来自URI的路径,现在想要从中创建一个文件但是收到错误。无法解决这个问题。请帮忙

                String encodedImageString = null;
             Uri selectimage = intent.getData();
             String selectedImagepath = getPath(selectimage);

    File Bitmapimg=new File(selectedImagepath);


    private String getPath(Uri selectedImage) {
     String[] filePathColumn = { MediaStore.Images.Media.DATA };
     Cursor cursor = context.getContentResolver().query(
             selectedImage, filePathColumn, null, null, null);
     cursor.moveToFirst();
     int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
     String filePath = cursor.getString(columnIndex);
     cursor.close();}

logcat的:

05-09 11:39:42.455: E/AndroidRuntime(28364): FATAL EXCEPTION: main
05-09 11:39:42.455: E/AndroidRuntime(28364): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://media/external/images/media/2746 (has extras) }} to activity {com.inspectreport6784/com.inspectreport6784.NewInspection}: java.lang.NullPointerException
05-09 11:39:42.455: E/AndroidRuntime(28364):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3431)
05-09 11:39:42.455: E/AndroidRuntime(28364):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3474)
05-09 11:39:42.455: E/AndroidRuntime(28364):    at android.app.ActivityThread.access$1200(ActivityThread.java:152)
05-09 11:39:42.455: E/AndroidRuntime(28364):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1330)
05-09 11:39:42.455: E/AndroidRuntime(28364):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-09 11:39:42.455: E/AndroidRuntime(28364):    at android.os.Looper.loop(Looper.java:137)
05-09 11:39:42.455: E/AndroidRuntime(28364):    at android.app.ActivityThread.main(ActivityThread.java:5328)
05-09 11:39:42.455: E/AndroidRuntime(28364):    at java.lang.reflect.Method.invokeNative(Native Method)
05-09 11:39:42.455: E/AndroidRuntime(28364):    at java.lang.reflect.Method.invoke(Method.java:511)
05-09 11:39:42.455: E/AndroidRuntime(28364):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
05-09 11:39:42.455: E/AndroidRuntime(28364):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
05-09 11:39:42.455: E/AndroidRuntime(28364):    at dalvik.system.NativeStart.main(Native Method)
05-09 11:39:42.455: E/AndroidRuntime(28364): Caused by: java.lang.NullPointerException
05-09 11:39:42.455: E/AndroidRuntime(28364):    at java.io.File.fixSlashes(File.java:185)
05-09 11:39:42.455: E/AndroidRuntime(28364):    at java.io.File.<init>(File.java:134)
05-09 11:39:42.455: E/AndroidRuntime(28364):    at com.inspectreport6784.NewInspection.onActivityResult(NewInspection.java:505)
05-09 11:39:42.455: E/AndroidRuntime(28364):    at android.app.Activity.dispatchActivityResult(Activity.java:5439)
05-09 11:39:42.455: E/AndroidRuntime(28364):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3427)
05-09 11:39:42.455: E/AndroidRuntime(28364):    ... 11 more

1 个答案:

答案 0 :(得分:0)

您需要检查光标是否为空。

private String getPath(Uri selectedImage) {
 String[] filePathColumn = { MediaStore.Images.Media.DATA };
 Cursor cursor = context.getContentResolver().query(
         selectedImage, filePathColumn, null, null, null);
 String filePath = "";
 int columnIndex = 0;
 if (cursor.moveToFirst()) {
      columnIndex = cursor.getColumnIndex(filePathColumn[0]);
      filePath = cursor.getString(columnIndex);
      cursor.close();}
 }