![](/img/trans.png)
[英]Difference between getExternalStorageDirectory () and getExternalStorageState ()
[英]Why does the recommended `getExternalStorageState()` to replace deprecated `getExternalStorageDirectory()` not work correctly?
問題:當我使用推薦的getExternalStorageState()
替換已棄用的getExternalStorageDirectory()
時,推薦的替換返回錯誤的路徑。 當FileInputStream
崩潰時,我發現了這個問題,這是以前沒有的。
以下內容已棄用但正確返回: /storage/emulated/0/Download/alaina.jpg
return Environment.getExternalStorageDirectory().toString() + "/Download/" + fileName;
以下推薦並返回: mounted/download/alaina.jpg
return Environment.getExternalStorageState() + "/Download/" + fileName;
我所做的:我已經導入並使用了RealPathUtil
的 class RealPathUtil,它在此處找到並且效果很好。 但是,現在不推薦使用Environment
和MediaStore
的某些方法。 通過警告建議使用getExternalStorageState()
。 我通常會清理警告並使用建議,這是第一個導致問題的地方。
模擬器位置:
當我使用推薦的 getExternalStorageState() 替換已棄用的 getExternalStorageDirectory() 時,
這是沒有意義的,因為您無法將它們相互比較。
一個提供文件路徑。 另一個是 state。
無需使用 getExternalStorageState() 因為它總是返回 state 安裝。
多年來,始終有可用的外部存儲。
方法 getExternalStorageDirectory() 有效,一周前未棄用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.