Java Android 在 SD 卡上 mkdir() 失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20114206/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android failed to mkdir() on SD card
提问by Allan Jiang
I have some code to create/get a folder on SD card:
我有一些代码可以在 SD 卡上创建/获取文件夹:
if( hasSDCard() ){
UUID uniqueFileName = UUID.randomUUID();
mediaStorageDir = new File(
getExternalImageStoragePath(),
"MyApp");
if ( ! mediaStorageDir.exists() ){
if( ! mediaStorageDir.mkdirs() ){
MyLogger.Error("Create image directory FAILED. path: " + mediaStorageDir.getPath());
return null;
}
}
and I have the permission registered in my Manifest file:
并且我在我的清单文件中注册了权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STOREAGE"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
but when I run it, it prints:
但是当我运行它时,它会打印:
Create image directory FAILED. path: /storage/emulated/0/Pictures/MyApp
I was wondering why this happens?
我想知道为什么会发生这种情况?
=== UPDATE ===
=== 更新 ===
I have tried all of them:
我已经尝试了所有这些:
/**
* get external storage directory path for image
* @return
*/
public static String getExternalImageStoragePath(){
String strPath = "";
if(hasSDCard()){
//strPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getPath();
//strPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath();
//strPath = Environment.getExternalStorageDirectory().getPath();
strPath = Environment.getExternalStorageDirectory().getAbsolutePath();
}
return strPath;
}
But they all behave exactly the same...
但他们的行为完全一样......
=== UPDATE 2 ===
=== 更新 2 ===
I am testing with Nexus 5. I saw this post Cannot find storage/emulated/0/ folder of Nexus 7 in EclipseThere might be some issue?
我正在使用 Nexus 5 进行测试。我看到这篇文章在 Eclipse 中找不到 Nexus 7 的存储/模拟/0/ 文件夹可能有问题?
=== UPDATE 3 ===
=== 更新 3 ===
>> adb shell ls -l /storage/
dr-xr-xr-x root root 1970-01-24 23:48 emulated
lrwxrwxrwx root root 1970-01-24 23:48 sdcard0 -> /storage/emulated/legacy
>> adb shell ls -l /storage/emulated/0/Pictures/
/storage/emulated/0/Pictures/: No such file or directory
=== UPDATE 4 ===
=== 更新 4 ===
Here is my hasSD()
method:
这是我的hasSD()
方法:
public static boolean hasSDCard(){
boolean fHasSDCard = false;
if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())){
fHasSDCard = true;
}
return fHasSDCard;
}
采纳答案by Melquiades
EDIT: you have misspelled your permission (you have STOREAGE, whereas the correct one is STORAGE)
编辑:您拼错了您的许可(您有 STOREAGE,而正确的是 STORAGE)
Change
改变
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STOREAGE"/>
to:
到:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Unless you're not doing it already in hasSDCard(), please check if your external media is available at all. As the docssay:
除非您还没有在 hasSDCard() 中这样做,否则请检查您的外部媒体是否可用。正如文档所说:
Before you do any work with the external storage, you should always call getExternalStorageState() to check whether the media is available. The media might be mounted to a computer, missing, read-only, or in some other state.
在使用外部存储进行任何工作之前,您应该始终调用 getExternalStorageState() 来检查媒体是否可用。介质可能已安装到计算机、丢失、只读或处于其他某种状态。
An example they provide:
他们提供的一个例子:
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;
}
回答by Haresh Chhelana
// try this
File mediaStorageDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + "MyApp" + "/");
if (!mediaStorageDir.exists()) {
mediaStorageDir.mkdirs();
}
mediaStorageDir.getAbsolutePath()
回答by Misha Bhardwaj
Try this:
尝试这个:
mediaStorageDir = new File(
Environment.getExternalStorageDirectory().getAbsolutePath(),
"MyApp");
回答by Shakeeb Ayaz
This is what i am doing and its working for me
这就是我正在做的事情,它对我有用
File path = Environment.getExternalStorageDirectory();
String save_path = path.toString() + "/MyApp/";
File newFile=new File(save_path );
newFile.mkdirs();