get path from Google Drive URI

后端 未结 3 907
庸人自扰
庸人自扰 2021-02-04 11:49

I\'m using Android file selection and selecting files from app storage (images, videos, documents). I have an function \"getPath\" . Im getting path from uri. I have no problem

3条回答
  •  野性不改
    2021-02-04 12:09

    I have no problem with gallery images or download documents

    You will, on many devices.

    But when i select a file from google drive i cant get path

    There is no path. ACTION_GET_CONTENT does not allow the user to choose a file. It allows the user to choose a piece of content. That content might be a local file. That content might also be:

    • Something on a file server on the network
    • Something in a cloud storage service, such as Google Drive, and therefore is not on the device at the moment
    • Something in an encrypted volume
    • Something that is a file, but is not a file that you can access directly, such as from internal storage of another app
    • And so on

    You have two main options. If you only want files, then use a third-party file chooser library to replace all of the code in your question.

    Or, if you still want to use ACTION_GET_CONTENT or ACTION_OPEN_DOCUMENT, you can take the Uri that you get from data.getData() in onActivityResult() and do two things with it:

    • First, use DocumentFile.fromSingleUri() to get a DocumentFile object pointing to that Uri. You can call getName() on the DocumentFile to get a "display name" for the content, which should be something that the user will recognize.

    • Then, use a ContentResolver and openInputStream() to get at the content itself, similar to how you might use a FileInputStream to get at the bytes in a file.

提交回复
热议问题