简体   繁体   English

如何使用 Context.getExternalFilesDirs(String)、Context.getExternalCacheDirs() 获取 /storage/emulated/0

[英]How to get the /storage/emulated/0 using Context.getExternalFilesDirs(String), Context.getExternalCacheDirs()

if i use the Environment.getExternalStorageDirectory() , i am getting the /storage/emulated/0 correctly.如果我使用Environment.getExternalStorageDirectory() ,我得到 /storage/emulated/0 正确。 But the getExternalStorageDirectory is get deprecated so, i am trying to achive this using the Context.getExternalFilesDirs(String), Context.getExternalCacheDirs() .但是 getExternalStorageDirectory 已被弃用,所以我试图使用Context.getExternalFilesDirs(String), Context.getExternalCacheDirs()来实现这一点。 i am getting like /storage/emulated/0/Android/data/com.example.statussaver .我越来越喜欢/storage/emulated/0/Android/data/com.example.statussaver basically it return the current application file directory.基本上它返回当前的应用程序文件目录。 How to get the /storage/emulated/0 using Context Api.如何使用上下文 Api 获取 /storage/emulated/0。 Thanks in advance提前致谢

You don't want to get that value anymore, because from the documentation您不想再获得该值,因为从文档

When an app targets Build.VERSION_CODES.Q , the path returned from this method is no longer directly accessible to apps当应用程序以Build.VERSION_CODES.Q为目标时,从此方法返回的路径不再可供应用程序直接访问

That means even if you have gotten the same path it will be useless to you.这意味着即使你走上了同样的道路,它对你也毫无用处。 Depending on your use case you should use one of the strategies outlined in https://developer.android.com/training/data-storage/shared instead.根据您的用例,您应该改用https://developer.android.com/training/data-storage/shared中列出的策略之一。

Android provides APIs for storing and accessing the following types of shareable data: Android 提供 API 用于存储和访问以下类型的可共享数据:

  • Media content: The system provides standard public directories for these kinds of files, so the user has a common location for all their photos, another common location for all their music and audio files, and so on.媒体内容:系统为这些类型的文件提供标准的公共目录,因此用户可以为所有照片提供一个公共位置,为所有音乐和音频文件提供另一个公共位置,等等。 Your app can access this content using the platform's MediaStore API.您的应用可以使用平台的MediaStore API 访问此内容。
  • Documents and other files: The system has a special directory for containing other file types, such as PDF documents and books that use the EPUB format.文档和其他文件:系统有一个特殊的目录用于包含其他文件类型,例如使用 EPUB 格式的 PDF 文档和书籍。 Your app can access these files using the platform's Storage Access Framework.您的应用可以使用平台的存储访问框架访问这些文件。
  • Datasets: On Android 11 (API level 30) and higher, the system caches large datasets that multiple apps might use.数据集:在 Android 11(API 级别 30)及更高版本上,系统缓存多个应用程序可能使用的大型数据集。 These datasets can support use cases like machine learning and media playback.这些数据集可以支持机器学习和媒体播放等用例。 Apps can access these shared datasets using the BlobStoreManager API.应用可以使用BlobStoreManager API 访问这些共享数据集。

For more information about these APIs, see the following guides:有关这些 API 的更多信息,请参阅以下指南:

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

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