2014-02-10 124 views
7

我有此代碼可以將文件上傳到我的應用程序,並且當使用文件管理器或Dropbox或其他任何文件打開文件時,返回的路徑是正確的,我可以訪問它,我只有谷歌驅動器的問題,它返回一些以「exposed_content」開頭的路徑,我不能「解碼」任何方式,我已經搜索並沒有找到方法,任何人有任何想法?Android獲取Uri從Google Drive中獲取文件的路徑

if (resultCode == Activity.RESULT_OK) { 
      if ((data != null) && (data.getData() != null)) { 
       final Uri filePath; 
       if (data.getDataString().startsWith("content")) { 
        filePath = getRealPathFromURI(getApplicationContext(), data.getData()); 
       } else { 
        filePath = data.getData(); 
       } 
       // TODO bug with google drive 
       if (filePath.getLastPathSegment() != null) { 
        tvSelectedFile.setText("File selected: " + filePath.getLastPathSegment()); 

       } else { 
        tvSelectedFile.setText("File can not be accessed, please try another way"); 
       } 

      } 
} 

回答

-1

使用附帶的代碼......從onActivity結果你會得到內容URI ......這個URI傳遞給指定的方法...

public static String getGDriveDataColumn(Context context, Uri uri, String selection, 
              String[] selectionArgs) { 
    Cursor cursor = null; 
    final String column = "_display_name"; 
    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(); 
    } 
      return null;  

} 
+1

什麼,我應該通過作爲選擇和selectionArgs兩個?我嘗試傳遞一個空值,但我只獲取文件名,而不是文件的路徑。 – jcesarmobile

+0

我還想知道@jcesarmobile提問的內容。答案並不清楚。 – Nom1fan

+1

是的,我測試了這段代碼。它只給出文件名 – FaisalAhmed