选择图像时,处理Android Image会返回NullPointerException

时间:2014-05-19 05:44:23

标签: android nullpointerexception

我有一个带有按钮的Android应用程序,您可以在其中选择图像。单击图像后,它将获取值并将其传递到onActivityResult()

是这行代码我总是收到错误NullPointerException

 Uri uri = data.getData();
 String path = FileUtil.getRealPath(this, uri);
 mPhotoUri = Uri.parse(path);

uri的值为content://com.android.providers.media.documents/document/image%3A319为什么会出现此错误?

日志

05-19 16:20:18.679: E/galleryselector(13660): java.lang.NullPointerException: uriString
05-19 16:20:18.679: E/galleryselector(13660):   at android.net.Uri$StringUri.<init>(Uri.java:467)
05-19 16:20:18.679: E/galleryselector(13660):   at android.net.Uri$StringUri.<init>(Uri.java:457)
05-19 16:20:18.679: E/galleryselector(13660):   at android.net.Uri.parse(Uri.java:429)
05-19 16:20:18.679: E/galleryselector(13660):   at com.galleryselector.PostActivity.onActivityResult(PostActivity.java:253)
05-19 16:20:18.679: E/galleryselector(13660):   at android.app.Activity.dispatchActivityResult(Activity.java:5423)
05-19 16:20:18.679: E/galleryselector(13660):   at android.app.ActivityThread.deliverResults(ActivityThread.java:3361)
05-19 16:20:18.679: E/galleryselector(13660):   at android.app.ActivityThread.handleSendResult(ActivityThread.java:3408)
05-19 16:20:18.679: E/galleryselector(13660):   at android.app.ActivityThread.access$1300(ActivityThread.java:135)
05-19 16:20:18.679: E/galleryselector(13660):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
05-19 16:20:18.679: E/galleryselector(13660):   at android.os.Handler.dispatchMessage(Handler.java:102)
05-19 16:20:18.679: E/galleryselector(13660):   at android.os.Looper.loop(Looper.java:136)
05-19 16:20:18.679: E/galleryselector(13660):   at android.app.ActivityThread.main(ActivityThread.java:5017)
05-19 16:20:18.679: E/galleryselector(13660):   at java.lang.reflect.Method.invokeNative(Native Method)
05-19 16:20:18.679: E/galleryselector(13660):   at java.lang.reflect.Method.invoke(Method.java:515)
05-19 16:20:18.679: E/galleryselector(13660):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-19 16:20:18.679: E/galleryselector(13660):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-19 16:20:18.679: E/galleryselector(13660):   at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:126)
05-19 16:20:18.679: E/galleryselector(13660):   at dalvik.system.NativeStart.main(Native Method)

FileUtil类

  public static String getRealPath(Context context, Uri uri) {
    String[] projection = { MediaColumns.DATA };
    Cursor cursor = context.getContentResolver().query(uri, projection, null, null, null);

    cursor.moveToFirst();

    int columnIndex = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
    String path = cursor.getString(columnIndex);

    cursor.close();

    return path;
  }

当我使用KITKAT进行调试时出现

1 个答案:

答案 0 :(得分:0)

试试这个,

getAbsolutePath(data.getData()) + "|").split("\\|")


public String getAbsolutePath(Uri uri) {
        String[] projection = { MediaColumns.DATA };
        @SuppressWarnings("deprecation")
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        if (cursor != null) {
            int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        } else
            return null;
    }

这可能会对你有所帮助