我有一些代码可以在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;
}
}
我已在我的清单文件中注册了该权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STOREAGE"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
但是当我运行它时,会打印出来:
Create image directory FAILED. path: /storage/emulated/0/Pictures/MyApp
我想知道为什么会这样?
=== UPDATE ===
我尝试了所有这些:
/**
* 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;
}
但他们的行为完全一样......
===更新2 ===
我正在使用Nexus 5进行测试。我看过这篇文章Cannot find storage/emulated/0/ folder of Nexus 7 in Eclipse可能存在一些问题?
===更新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
===更新4 ===
这是我的hasSD()
方法:
public static boolean hasSDCard(){
boolean fHasSDCard = false;
if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())){
fHasSDCard = true;
}
return fHasSDCard;
}
答案 0 :(得分:9)
编辑:你拼错了你的许可(你有STOREAGE,而正确的是STORAGE)
更改
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STOREAGE"/>
为:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
除非你在hasSDCard()中没有这样做,否则请检查你的外部媒体是否可用。正如docs所说:
在使用外部存储器进行任何操作之前,应始终调用getExternalStorageState()来检查介质是否可用。媒体可能安装在计算机上,丢失,只读或处于其他状态。
他们提供的一个例子:
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;
}
答案 1 :(得分:0)
// try this
File mediaStorageDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + "MyApp" + "/");
if (!mediaStorageDir.exists()) {
mediaStorageDir.mkdirs();
}
mediaStorageDir.getAbsolutePath()
答案 2 :(得分:0)
试试这个:
mediaStorageDir = new File(
Environment.getExternalStorageDirectory().getAbsolutePath(),
"MyApp");
答案 3 :(得分:0)
这就是我正在做的事情,它为我工作
File path = Environment.getExternalStorageDirectory();
String save_path = path.toString() + "/MyApp/";
File newFile=new File(save_path );
newFile.mkdirs();