2014-05-19 60 views
0

我有這個android應用程序與一個按鈕,您可以選擇圖像。一旦你點擊圖像,將獲得的價值,並把它傳遞onActivityResult()處理Android圖像時選擇圖像返回NullPointerException

是此行的代碼,我總是得到一個錯誤NullPointerException

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

uricontent://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; 
    } 

這似乎當調試使用奇巧

+0

可以請您與我們分享logcat嗎? – Opiatefuchs

+0

'FileUtil.getRealPath(this,uri)'返回'null'。什麼是FileUtil類?請爲此顯示導入 – gerbit

回答

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; 
    } 

這可能會幫助你