简体   繁体   English

从外部存储读取硬编码的 Uri

[英]Read harcoded Uri from external storage

Im trying to read a json file from an external storage without sucess.我试图从外部存储读取 json 文件但没有成功。 Im getting the error:我收到错误:

Permission Denial: opening provider com.android.externalstorage.ExternalStorageProvider from ProcessRecord{bfaa1d6 26301:com.plm.valdecillasurdemo/u0a284} (pid=26301, uid=10284) requires that you obtain access using ACTION_OPEN_DOCUMENT or related API
  1. My manifest.xml file is OK我的 manifest.xml 文件没问题
  2. I am checking in runtime for READ_EXTERNAL_STORAGE permission and only read the file when the permission is granted我正在检查运行时的 READ_EXTERNAL_STORAGE 权限,并且仅在授予权限时读取文件
  3. When I try read the file I getting the related error.当我尝试读取文件时,出现相关错误。

In a second approach I started an ACTION_OPEN_DOCUMENT Intent.在第二种方法中,我启动了 ACTION_OPEN_DOCUMENT 意图。

A system dialog ask me for picking a file and all is fine.一个系统对话框要求我选择一个文件,一切都很好。 I read the file correctly however I dont want be asked for a Uri.我正确阅读了文件,但是我不想被要求提供 Uri。 My Uri is hardcoded.我的 Uri 是硬编码的。

I know must be easy the solution but this problem is drives me crazy...How can I resolve this?我知道解决方案一定很容易,但这个问题让我发疯......我该如何解决这个问题?

Thanks in advance.提前致谢。

My Uri is hardcoded我的 Uri 是硬编码的

That is not possible.这是不可能的。 You do not have rights to such a document, even if the Uri is valid (which it won't be, since the DocumentsProvider did not do any work on its side to grant it).即使Uri是有效的,您也无权使用此类文档(它不会是有效的,因为DocumentsProvider没有在其方面做任何工作来授予它)。 Beyond that, the user may not have a document at the location that you hard-coded.除此之外,用户可能在您硬编码的位置没有文档。

How can I resolve this?我该如何解决这个问题?

Use ACTION_OPEN_DOCUMENT and allow the user to choose the document to remove.使用ACTION_OPEN_DOCUMENT并允许用户选择要删除的文档。

Or, use filesystem locations that you can read and write across OS versions, mostly those defined on Context : getFilesDir() , getCacheDir() , getExternalFilesDir() , getExternalCacheDir() , etc. In this case, you will not need to work with Uri values.或者,使用可以跨操作系统版本读取和写入的文件系统位置,主要是在Context定义的位置: getFilesDir()getCacheDir()getExternalFilesDir()getExternalCacheDir()等。在这种情况下,您将不需要使用Uri值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM