Android的mkdirs()SD卡不工作 [英] Android mkdirs() sdcard do not work

查看:422
本文介绍了Android的mkdirs()SD卡不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在SD卡,使目录,我做如下:

I want to make dir in Sdcard, and i do follow:


  1. 我说:<使用许可权的android:NAME =android.permission.WRITE_EXTERNAL_STORAG​​E/> 在清单

  2. 我得到root_path:公共静态最后弦乐ROOT_PATH = Environment.getExternalStorageDirectory()的toString()+/参考hello world /; 键,返回
    /存储/模拟/ 0 /参考hello world (获得时调试)。

  1. I added: <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> in manifest.
  2. I get root_path by: public static final String ROOT_PATH = Environment.getExternalStorageDirectory().toString() + "/Hello_World/"; and it returns /storage/emulated/0/Hello_World (getting when debug).

接下来,我运行这个code:

Next, I run this code:

File file = new File(Constants.ROOT_PATH);
int i = 0;
while (!file.isDirectory() && !file.mkdirs()) {
    file.mkdirs();
    Log.e("mkdirs", "" + i++);
}

我也试过这两个 mkdirs()的mkdir(),但它显示在logcat中无限循环( Log.e(mkdirs,+ I ++); )。有时它工作,但有时没有。
感谢您的帮助!
结果更新:我想我的code对于某些设备:Nexus4,nexus7,维加熨斗,Genymotion,LG G预期临,那么就维加铁的工作。 ?? !!?!?

I also tried both mkdirs() and mkdir() but it's showing endless loop in logcat (Log.e("mkdirs", "" + i++);). Sometimes it work, but sometimes not. Thanks for you helping!
Update: I tried my code for some devices: Nexus4, nexus7, Vega Iron, Genymotion, LG G Pro, then just Vega Iron work as expected. ??!!?!?

推荐答案

尝试这样它会创建在 SD卡的文件夹

String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/hello_world");    
myDir.mkdirs();

如果您要检查该文件是否存在,或者不使用此code

If you want to check that file exists or not use this code

File file = new File (myDir, file_name);
if (file.exists ()) 
   // file exist 
else 
   // file not exist  

有关参考看看href=\"http://stackoverflow.com/a/7887114/964741\"> Android的文件保存到外部存储这个答案

For reference look at this answer Android saving file to external storage

这篇关于Android的mkdirs()SD卡不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆