2014-01-09 121 views
6

我想在我的SD卡文件夹中创建文件夹,我用下面的代码:如何在SD卡中的Android

public class Screen extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.welcome); 

     operateOnFirstUsage(); 
    } 

    private void operateOnFirstUsage() { 

     String state = Environment.getExternalStorageState(); 
     Log.d("Media State", state); 

     if (Environment.MEDIA_MOUNTED.equals(state)) { 
      File appDirectory = new File(
        Environment.getExternalStorageDirectory().getAbsolutePath() + "/MyApp/"); 

      Log.d("appDirectroyExist", appDirectory.exists() + ""); 
      if (!appDirectory.exists()) 
       Log.d("appDir created: ", appDirectory.mkdir() + ""); 

      File dbDirectory = new File(
        Environment.getExternalStorageDirectory().getAbsolutePath() + "/MyApp/Database/"); 

      Log.d("dbDirectroyExist", dbDirectory.exists() + ""); 
      if (!dbDirectory.exists()) 
       Log.d("dbDir created: ", dbDirectory.mkdirs() + ""); 


      File themesDirectory = new File(
        Environment.getExternalStorageDirectory().getAbsolutePath() + "/MyApp/Themes/"); 
      Log.d("themesDirectroyExist", themesDirectory.exists() + ""); 
      if (!themesDirectory.exists()) 
       Log.d("themesDir created: ", themesDirectory.mkdirs() + ""); 

     } 
    } 
} 

而且,我已经设置了SD卡写入权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 

我已经运行的应用程序几次,每次我得到的logcat输出:

01-09 21:38:13.701: D/Media State(15363): mounted 
01-09 21:38:13.701: D/appDirectroyExist(15363): false 
01-09 21:38:13.701: D/appDir created:(15363): false 
01-09 21:38:13.701: D/dbDirectroyExist(15363): false 
01-09 21:38:13.701: D/dbDir created:(15363): false 
01-09 21:38:13.701: D/themesDirectroyExist(15363): false 
01-09 21:38:13.701: D/themesDir created:(15363): false 

我看过类似的问题,但没有使用得到。我该怎么做才能让代码运行?我的问题是什么?

+0

File file = new File(Environment.getExternalStorageDirectory(),path);尝试这个。 – Triode

+0

只是创建一个'File'对象实际上并没有对SD卡做任何事情。如果你想创建一个目录,你需要在'File'对象上调用'mkdir()'。如果你想创建一个文件,你需要在该对象上调用'createNewFile()'。 –

+0

@RajeshCP我试过了,但结果相同。 –

回答

0

我不知道为什么应用程序不如此行事,但最后我通过删除并重新在Android清单文件中的SD卡的写入权限克服了这个问题。谢谢大家的亲切帮助,并为您抽出时间表示歉意。

+0

不客气。 – dakshbhatt21

1

这是我在图片文件夹OD的SD卡创建的文件夹MYDIR:

File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"MyDir"); 
mediaStorageDir.mkdirs(); 
+0

我使用了你的方法,带有两个参数的新文件以及你的确切代码。仍然没有好处。 –

+0

你尝试过不同的手机吗? – gile

1

试试这个方法:

File file=new File(Environment.getExternalStorageDirectory() + File.separator +"MyApp/"); 
file.mkdirs(); 
+0

我仍然在LogCat中出现错误,并且我也检查了SD卡以及没有创建文件夹。 –

+0

我已try.it正常工作。我有一个应用程序以相同的方式工作。 – Dharmendra

13

编辑

试试这个:

File mydir = new File(Environment.getExternalStorageDirectory() + "/mydir/"); 
if(!mydir.exists()) 
    mydir.mkdirs(); 
else 
    Log.d("error", "dir. already exists"); 

,并重新检查许可

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
+0

你看我已经在我的代码中完成了这项工作。我必须深入到问题的底部。 –

+0

只是好奇,为什么它不会更好地使用'Environment.getExternalStorageDirectory()'?硬编码是否适用于所有设备和未来的证明? – KickingLettuce

+1

@KickingLettuce:是的,你说得对,我们应该使用'Environment.getExternalStorageDirectory()',但是为了测试我使用'/ sdcard'并且它在我的所有模拟器和设备上都能正常工作。感谢您的建议,我编辑了我的答案。 – dakshbhatt21

1

试试这个代码:),它很简单。

 String fileName = "CallHistory.pdf"; 

     // create a File object for the parent directory 
     File PDF_Directory = new File("/sdcard/MOBstate/"); 

     // have the object build the directory structure, if needed.   
     PDF_Directory.mkdirs(); 

     // create a File object for the output file   
     File outputFile = new File(PDF_Directory, fileName); 

     // now attach the OutputStream to the file object, instead of a ring representation 
     FileOutputStream fos = new FileOutputStream(outputFile);