android mkdir失败_Android的失败的mkdir()SD卡

I have some code to create/get a folder on SD card:

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:

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 ===

I am testing with Nexus 5. I saw this post Cannot find storage/emulated/0/ folder of Nexus 7 in Eclipse There might be some issue?

=== UPDATE 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 ===

Here is my hasSD() method:

public static boolean hasSDCard(){

boolean fHasSDCard = false;

if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())){

fHasSDCard = true;

}

return fHasSDCard;

}

解决方案

EDIT: you have misspelled your permission (you have STOREAGE, whereas the correct one is STORAGE)

Change

to:

Unless you're not doing it already in hasSDCard(), please check if your external media is available at all. As the docs say:

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.

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;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值