get Path name from Uri Android Lollipop

*爱你&永不变心* 提交于 2019-12-04 05:30:56

问题


Lollipop version.

I used this code to determine file path from Uri. Works fine, but if I choose file from a Downloaded app - getRealPathFromURI_API19 function does not work for me. whole Id for this case equals /document/4 for example, and my app was crashed.

Ok, I made some changes for this function and "id" for Uri "/document/4" now equals 4, but function returns null.

Is it possible get file path for choosed image from any app? I need file path for ExifInterface.


回答1:


Works fine

Only for the few devices that you tried, for the one file source (MediaStore) that you tried.

Is it possible get file path for choosed image from any app?

No. A Uri is not a File. There is no requirement for any Uri to be convertible into file path, as the Uri may not represent a file, let alone a file that your app has the rights to access. To you, a Uri is an opaque address to content owned by another app, much as a URL is an opaque address to content owned by a Web server. And, just as you cannot magically convert a URL into a file path that you can use, you cannot magically convert a Uri into a file path that you can use.

I need file path for ExifInterface.

In reality, you need to read, or possibly write, EXIF headers associated with a Uri that you believe points to an image that contains those headers. While ExifInterface lets you do that, it is designed for apps to work with their own images, not images from third-party apps.

Your choices are:

  • Use ContentResolver and openInputStream() on the Uri, and make a local copy of the image, after which you can use ExifInterface

  • Use some other EXIF code that offers more flexibility. For example, the EXIF classes from the AOSP Mms app can read EXIF headers from an InputStream



来源:https://stackoverflow.com/questions/29699859/get-path-name-from-uri-android-lollipop

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