Google Drive GET_CONTENT intent read file

感情迁移 提交于 2019-12-03 01:46:31
Orkun Koçyiğit

It is sending you uri of content provider which you can use it with ContentResolver,for example such as:

getContentResolver().query(Uri contentUri, String[] projection, String selection, String[] selectionArgs, String sortOrder);

Edit: For getting real path names use the solution provided below

Android: Getting a file URI from a content URI?

Jahangeer Ahmed

I was also facing the same problem. I used follow code to get file path from Google Drive file. It works for SkyDrive and also for DropBox.

String filePath = null;
Uri _uri = data.getData();
Log.d("", "URI = " + _uri);                                       
if(_uri != null && "content".equals(_uri.getScheme()))  {
    Cursor cursor = this.getContentResolver().query(_uri, new String[] { android.provider.MediaStore.Files.FileColumns.DATA }, null, null, null);
    cursor.moveToFirst();   
    filePath = cursor.getString(0);
    cursor.close();
} else {
     filePath = _uri.getPath();
}
Log.d("", "Chosen path = " + filePath);

I'm using the intent to choose the file. Here is my code to choose the file.

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
String strType = "*/*";
intent.setDataAndType(Uri.parse(dir.getAbsolutePath()), strType);
startActivityForResult(intent, PICKFILE_RESULT_CODE);

My code works fine and when I get file from internal or external memory. I want to get file from Google Drive with same code.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!