Android在内存中创建文件夹

时间:2011-11-14 16:21:50

标签: android storage internal

是否允许在Android内部存储器中创建文件夹?示例:

- data
-- com.test.app (application's main package)
---databases (database files)
---files (private files for application)
---shared_prefs (shared preferences)

---users (folder which I want to create)

我可以在内存中创建users文件夹吗?

6 个答案:

答案 0 :(得分:40)

我用它在内存中创建文件夹/文件:

File mydir = context.getDir("mydir", Context.MODE_PRIVATE); //Creating an internal dir;
File fileWithinMyDir = new File(mydir, "myfile"); //Getting a file within the dir.
FileOutputStream out = new FileOutputStream(fileWithinMyDir); //Use the stream as usual to write into the file.

答案 1 :(得分:14)

如果您希望用户轻松访问您的文件,则不应使用此

File newdir= context.getDir("DirName", Context.MODE_PRIVATE);  //Don't do
if (!newdir.exists())
    newdir.mkdirs();

INSTEAD,执行此操作:

要在手机主存储器(通常是内部存储器)上创建目录,您应该使用以下代码。请注意, Environment.getExternalStorageDirectory()中的ExternalStorage不一定是指sdcard,它返回手机主存储器

File mediaStorageDir = new File(Environment.getExternalStorageDirectory(), "MyDirName");

if (!mediaStorageDir.exists()) {
    if (!mediaStorageDir.mkdirs()) {
        Log.d("App", "failed to create directory");
    }
}

使用此代码创建的目录将很容易被手机用户看到。另一种方法(首先提到)在位置创建目录(/data/data/package.name/app_MyDirName),因此普通电话用户将无法轻松访问它,因此您不应该使用它来存储视频/照片等。

您需要AndroidManifest.xml中的权限

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

答案 2 :(得分:4)

context.getDir(“mydir”,...);这会创建your.package / app_mydir /

 /** Retrieve or creates <b>path</b>structure inside in your /data/data/you.app.package/
 * @param path "dir1/dir2/dir3"
 * @return
 */
private File getChildrenFolder(String path) {
            File dir = context.getFilesDir();
    List<String> dirs = new ArrayList<String>(Arrays.<String>asList(path.split("/")));
    for(int i = 0; i < dirs.size(); ++i) {
        dir = new File(dir, dirs.get(i)); //Getting a file within the dir.
        if(!dir.exists()) {
            dir.mkdir();
        }
    }
    return dir;
}

答案 3 :(得分:1)

Android更改了他关于存储的安全性, 有关更多详细信息,请观看视频Storage access with Android 10

您也可以在android 10中尝试这个

File mydir = new File(getApplicationContext().getExternalFilesDir("Directory Name").getAbsolutePath());
    if (!mydir.exists())
    {
        mydir.mkdirs();
        Toast.makeText(getApplicationContext(),"Directory Created",Toast.LENGTH_LONG).show();
    }

目录路径将在您的应用数据中。

答案 4 :(得分:0)

  private File createFile(String fName)  {
            String filePath = Environment.getExternalStorageDirectory().getAbsolutePath()+"/" + fName+".wav";
            File newFile = new File(filePath);
            println(":File path = "+newFile+"\n "+filePath);
            if (!newFile.exists()) {
                try {
                    newFile.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                    toastMsg("Unable to Create New File !");
                }
            } else {
                toastMsg("File Already Exists");

            }

            return newFile;

        }

//请求多个权限

private void reqMulPerm(){
        String permissions[]={
                Manifest.permission.ACCESS_BACKGROUND_LOCATION,
                Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.ACCESS_COARSE_LOCATION,
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
        };

        if(permissions.length!=0){
            ActivityCompat.requestPermissions(this,permissions,1);
        }
    }

答案 5 :(得分:0)

File f = new File(Environment.getExternalStorageDirectory(), "FolderName");
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            try {
               Files.createDirectory(Paths.get(f.getAbsolutePath()));
            } catch (IOException e) {
                e.printStackTrace();
                Toast.makeText(getApplicationContext(), e.getLocalizedMessage(), Toast.LENGTH_LONG).show();
            }
        } else {
            f.mkdir();
            f.mkdirs();
            Toast.makeText(getApplicationContext(), f.getPath(), Toast.LENGTH_LONG).show();
        }