17

I have an app on android that does some file sharing using cloud storages like dropbox. To start sharing I throw android.intent.action.SEND. On the list that is shown I see the Google Drive app (previously installed), so I try sending the file to it - it works ok, the file appears in the Drive list.

Then, on another device I want to read this file. I throw the android.intent.action.GET_CONTENT intent, choose Drive and then don't know how to get to file. I receive an Uri something like this:

content://com.google.android.apps.docs.files/exposed_content/6jn9cnzdJbDywpza%2BlW3aA%3D%3D%0A%3BRV%2FaV94o%2FCcW4HGBYArtwOdHqt%2BrsYO4WmHcs6QWSVwp%2FXogkRAgit7prTnfp00a%0A

which I don't know how to transform to physical file path. How could I get the file contents from this?

I played around the content provider and can get the file name, but not the full path or anything else.

For the dropbox I get the file:// style uri, straight and simple, works well.

MPelletier
  • 16,256
  • 15
  • 86
  • 137
khusrav
  • 5,267
  • 5
  • 27
  • 38

2 Answers2

5

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?

Community
  • 1
  • 1
Orkun Kocyigit
  • 1,137
  • 10
  • 21
  • yes, but I can only get the file name, but not the path to it. – khusrav Jul 27 '13 at 11:49
  • http://stackoverflow.com/questions/5657411/android-getting-a-file-uri-from-a-content-uri provides an example how to do that in your case of question. – Orkun Kocyigit Jul 27 '13 at 11:52
  • Simply, this does not work. Real answer is here: https://developers.google.com/drive/quickstart-android – Nizzy Dec 12 '13 at 07:59
  • it does work. I don't want to make integration with Drive besides firing intents, so the above approach is ok for me. – khusrav Dec 17 '13 at 11:47
2

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.

rekire
  • 47,260
  • 30
  • 167
  • 264