文件夹创建错误

时间:2015-04-17 09:03:32

标签: android

我使用以下代码在图片文件夹中创建一个文件夹并烘烤路径。 但是我没有创建文件夹,我已经提到在android清单中写入外部存储。

    java.io.File file = new java.io.File (Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"College");
    if(!file.exists())
    {
        if(file.mkdir())
        {

            String f =file.getAbsolutePath();
            Toast.makeText(getApplicationContext(),f,Toast.LENGTH_LONG).show();
        }
        else {
            Toast.makeText(getApplicationContext(),"File Not Created",Toast.LENGTH_LONG).show();

        }

    }

请帮我解决问题

2 个答案:

答案 0 :(得分:0)

这对我有用:

import java.io.File;

File file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES + "/College");

    try
    {
        if (!file.exists()) {
            file.mkdir();
            Toast.makeText(this, "Folder created at " + file.toString(), Toast.LENGTH_LONG).show();
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }

答案 1 :(得分:0)

我使用了这段代码并且有效

    File file;
    String CAMERA_DIR = "/dcim/";

    // Check that the SDCard is mounted
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {

        // Get the absolute path 
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
            file = new File(Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES), "College");
        }
        else {
            file = new File( Environment.getExternalStorageDirectory() + CAMERA_DIR + "College");
        }


        // Create the storage directory if it does not exist
        if (! file.exists()) {
             if (! file.mkdirs()) {
                 Toast.makeText(getApplicationContext(),"File Not Created",Toast.LENGTH_LONG).show();
                 return;
             }
        }

         String f =file.getAbsolutePath();
        Toast.makeText(getApplicationContext(),f,Toast.LENGTH_LONG).show();
    }
    else {
        throw new IOException();
    }

}