[英]Write permission for external sd card in android 4.0
在嘗試將文件寫入android 4.0.3
外部SD卡時我遇到了問題,即使我已經使用了WRITE_EXTERNAL_STORAGE
和WRITE_MEDIA_STORAGE
類的寫權限,所以我找到了另一種方法來處理它,但現在我想要找出我的應用程序是否可以寫入可移動SD卡,如果沒有,那么我想切換到內部SD卡。
所以現在問題是我如何知道是否支持寫入可移動SD卡,所以如果有人做過任何類似的實現,請隨時在這里分享代碼。
直接從API指南中回答:
http://developer.android.com/guide/topics/data/data-storage.html#filesExternal
boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
// We can read and write the media
mExternalStorageAvailable = mExternalStorageWriteable = true;
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
// We can only read the media
mExternalStorageAvailable = true;
mExternalStorageWriteable = false;
} else {
// Something else is wrong. It may be one of many other states, but all we need
// to know is we can neither read nor write
mExternalStorageAvailable = mExternalStorageWriteable = false;
}
我有一個應用程序寫入內部和外部SD卡。 我有WRITE_EXTERNAL_STORAGE權限,但沒有別的。
處理外部SD卡的主要問題是找到路徑,因為getExternalStorageDirectory()
通常返回內部SD卡。 它應該是/mnt
的子目錄,如果有幫助的話。
要查找是否可以寫入SD卡,請查看Environment
類。
String externalStorageState = Environment.getExternalStorageState();
然后,您可以使用檢查此狀態
Environment.MEDIA_MOUNTED(externalStorageState)
和/或Environment.MEDIA_MOUNTED_READ_ONLY
(externalStorageState)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.