2016-08-02 48 views
0

我在SO herehere中看到過類似的問題。沒有從安卓照片應用獲取圖片

但仍然這些沒有解決我的問題,所以我發佈了一個新的問題。

這裏是我的代碼,以獲得在照片應用程序中選擇的圖像的路徑。但我沒有得到cursor.getColumnIndexOrThrow(column);值爲和cursor.getString(column_index)值爲null

Cursor cursor = null; 
     final String column = "_data"; 
     final String[] projection = { 
       column 
     }; 

     try { 
      cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, 
        null); 
      if (cursor != null && cursor.moveToFirst()) { 
       final int column_index = cursor.getColumnIndexOrThrow(column); 
       return cursor.getString(column_index); 
      } 
     } finally { 
      if (cursor != null) 
       cursor.close(); 
     } 

任何人都可以幫我解決這個問題。如果我的問題太寬泛,請告訴我。我會更新我的問題。

UPDATE: 啓動意圖:

Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
intent.setType("image/*"); 
startActivityForResult(Intent.createChooser(intent, getString(R.string.selectPhoto)), RESULT_LOAD_IMAGE); 
在onActivityResult

:以下代碼

if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) { 

    String[] projection = {MediaStore.Images.Media.DATA}; 
    Cursor cursor = getContentResolver().query(contentURI, projection, null, null, null); 
    if (cursor == null) { 
      return contentURI.getPath(); 
    } else { 
     cursor.moveToFirst(); 
     int idx = cursor.getColumnIndex(projection[0]); 
     return cursor.getString(idx); 
    } 
} 

問候

回答

0

用戶

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     if (resultCode == RESULT_OK) { 
      switch (requestCode) { 
       case Common.Action_Gallery: 
        if (data != null) { 
         Uri selectedImage = data.getData(); 
         String[] filePathColumn = {MediaStore.Images.Media.DATA}; 
         Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null); 
         cursor.moveToFirst(); 
         int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
         String picturePath = cursor.getString(columnIndex); 
         cursor.close();       
        } 
        break; 
      } 
     } 
} 
+0

仍然在cursor.getString(columnIndex);電話 – ImGenie

+0

我的data.getData給出的答覆爲「content://com.google.android.apps.photos.contentprovider/0/1/mediaKey%3A%2FAF1QipP_19ASFn4CF_8XmzBwXI4WRR9SSDTgR7ZopL2v/ACTUAL」 – ImGenie

+0

我相信你沒有改變過的代碼很多此代碼正在我的應用程序中工作並使用各種設備進行測試。 –