如何在android中创建文件夹

时间:2014-04-22 11:25:20

标签: android android-intent

我已经坚持在我的手机中创建文件夹(Micromax Canvas 2)。我无法创建文件夹。请在我错误的地方打电话给我。

File folder = new File(Environment.getExternalStorageDirectory() + "/Example");
        boolean success = true;
        if (!folder.exists()) {
            success = folder.mkdirs();
        }
        if (success) {
          Toast.makeText(getApplicationContext(), "Success", Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(getApplicationContext(), "Failure", Toast.LENGTH_LONG).show();
        }

权限:

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

6 个答案:

答案 0 :(得分:3)

File f1 = new File(getApplicationContext().getFilesDir()+"");
fol = new File(f1, "Images");
if(!fol.exists())
{
    fol.mkdir();
}

答案 1 :(得分:1)

 File folder = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Example");
    boolean success = true;
    if (!folder.exists()) {
        success = folder.mkdirs();
    }
    if (success) {
      Toast.makeText(getApplicationContext(), "Success", Toast.LENGTH_LONG).show();
    } else {
        Toast.makeText(getApplicationContext(), "Failure", Toast.LENGTH_LONG).show();
    }

使用 getExternalStorageDirectory()。getAbsolutePath()而不只是 getExternalStorageDirectory()

答案 2 :(得分:1)

我这样实施。而不是“加号(+)”写“逗号(,)”

 File imageFileFolder = new File(Environment.getExternalStorageDirectory(),
                                 "folder name");
 imageFileFolder.mkdir();

答案 3 :(得分:1)

首先在AndroidMenifest.xml文件中添加权限

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

然后添加FolderInfo.Class

package com.xxx.cg;

    import java.io.File;

    import android.os.Environment;

    public class FolderInfo {
        public static final String SDCARD;
        static {
            SDCARD = Environment.getExternalStorageDirectory().getAbsolutePath();
        }
        public static final String CG_FOLDER = SDCARD + "/CG";

        public static String ASSET_FOLDER = CG_FOLDER + "/assets";

        public static boolean createFolderForCG() {
            boolean exist = false;
            File dir = new File(CG_FOLDER);
            if (dir.exists()) {
                exist = true;
            } else {
                if (dir.mkdirs()) {
                    exist = true;
                }
            }
            return exist;
        }

        public static boolean createAssetsFolderForCG() {
            boolean exist = false;
            File dir = new File(ASSET_FOLDER);
            if (dir.exists()) {
                exist = true;
            } else {
                if (dir.mkdirs()) {
                    exist = true;
                }
            }
            return exist;
        }

        public static boolean createFolder(String folder) {
            boolean exist = false;
            File dir = new File(ASSET_FOLDER + "/" + folder);
            if (dir.exists()) {
                exist = true;
            } else {
                if (dir.mkdirs()) {
                    exist = true;
                }
            }
            return exist;
        }

    }

然后从您的活动中拨打电话。例如MainActivity.Class。

        FolderInfo.createFolderForCG();
    FolderInfo.createAssetsFolderForCG();
            FolderInfo.createFolder(subFolderName);
然后跑。您可以显示您的SD卡的CG /资产。 以及子文件夹显示CG / assets /.................

最好的运气!

答案 4 :(得分:0)

String downloadDirectory = "/folderName";
        String extStorageDirectory = Environment.getExternalStorageDirectory()
                .toString();
        File newDownloadDirectory = new File(extStorageDirectory
                + downloadDirectory);
        newDownloadDirectory.mkdir();

答案 5 :(得分:0)

我已经解决了问题..我在应用程序标记之后声明了<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />。所以它没有用。我在申请tag.Now文件夹之前声明了权限。