2016-08-02 45 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

我相信你没有改变过的代码很多此代码正在我的应用程序中工作并使用各种设备进行测试。 –