【问题标题】:How to create folder in sdcard in android如何在android的sdcard中创建文件夹
【发布时间】:2014-01-28 11:27:48
【问题描述】:

我想在我的 sdcard 中创建文件夹,我使用了以下代码:

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() + "");

        }
    }
}

另外,我已经设置了 sdcard 写入权限:

<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

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

【问题讨论】:

  • 文件 file = new File(Environment.getExternalStorageDirectory(), path);试试这个。
  • 仅仅创建一个File 对象实际上并没有对 SD 卡做任何事情。如果要创建目录,则需要在该 File 对象上调用 mkdir()。如果你想创建一个文件,你需要在那个对象上调用createNewFile()
  • @RajeshCP 我试过了,但结果一样。
  • 是的,我使用了 mkdir() 和 mkdirs(),正如您在上面的 Log.d(...) 中看到的那样。
  • 你可以检查外部存储的状态,是否可写?

标签: android sd-card writable


【解决方案1】:

这就是我在图片文件夹 od sdcard 中创建文件夹 MyDir 的方式:

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

【讨论】:

  • 我使用了您的方法,带有两个参数的新文件以及您的确切代码。仍然没有任何好处。
  • 你试过不同的手机吗?
【解决方案2】:

试试这个方法:

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

【讨论】:

  • 我在 LogCat 中仍然是错误的,我也仔细检查了 sdcard 并且没有创建文件夹。
  • 我试过了。它工作正常。我有一个应用程序工作方式相同。
【解决方案3】:

已编辑

试试这个:

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" />

【讨论】:

  • 你看我已经在我的代码中做到了这一点。我必须找到问题的根源。
  • 只是好奇为什么使用Environment.getExternalStorageDirectory() 不是更好?硬编码是否适用于所有设备并面向未来?
  • @KickingLettuce:是的,你是对的,我们应该使用Environment.getExternalStorageDirectory(),但为了测试我使用/sdcard,它在我所有的模拟器和设备中都能正常工作。感谢您的建议,我编辑了我的答案。
  • @AliAllahyar:如果您的问题得到解决,您可以通过单击投票下方的“右”号来将此答案标记为完整。
  • @dakshbhatt21 您可能需要将代码编辑为java File mydir = new File(Environment.getExternalStorageDirectory(), "/mydir/");
【解决方案4】:

我不知道为什么应用程序会这样,但最后我通过删除和重置 Android 清单文件中的 sdcard 写入权限克服了这个问题。感谢大家的热心帮助,并为耽误您的时间深表歉意。

【讨论】:

    【解决方案5】:

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

            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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-27
      • 1970-01-01
      • 2013-03-04
      相关资源
      最近更新 更多