简体   繁体   中英

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. But the getExternalStorageDirectory is get deprecated so, i am trying to achive this using the Context.getExternalFilesDirs(String), Context.getExternalCacheDirs() . i am getting like /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. 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

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.

Android provides APIs for storing and accessing the following types of shareable data:

  • 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.
  • 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. 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. These datasets can support use cases like machine learning and media playback. Apps can access these shared datasets using the BlobStoreManager API.

For more information about these APIs, see the following guides:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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