Android 在内存中创建文件夹

发布于 2024-12-16 02:21:22 字数 314 浏览 1 评论 0原文

有什么办法/是否允许在 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 文件夹吗?

Is there any way/ is it allowed to create folders in Internal Memory in Android. Example :

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

Can I create users folder in Internal Memory for example?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

层林尽染 2024-12-23 02:21:22

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

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.

I used this to create folder/file in internal memory :

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.
我要还你自由 2024-12-23 02:21:22

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

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

,而是执行以下操作:

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

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

YOU SHOULD NOT USE THIS IF YOU WANT YOUR FILES TO BE ACCESSED BY USER EASILY

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

INSTEAD, do this:

To create directory on phone primary storage memory (generally internal memory) you should use following code. Please note that ExternalStorage in Environment.getExternalStorageDirectory() does not necessarily refers to sdcard, it returns phone primary storage memory

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

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

Directory created using this code will be visible to phone user easily. The other method (mentioned first) creates directory in location (/data/data/package.name/app_MyDirName), hence normal phone user will not be able to access it easily and so you should not use it to store video/photo etc.

You will need permissions, in AndroidManifest.xml

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
心在旅行 2024-12-23 02:21:22

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

context.getDir("mydir", ...); This creates 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;
}
落墨 2024-12-23 02:21:22

Android改变了他关于存储的安全性,
如需了解更多详情,请观看视频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();
    }

你的目录路径将在你的应用程序数据中。

Android change his security about Storage,
For more details check the video Storage access with Android 10

Also you can try this in 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();
    }

The path of your directory will be in your app data.

诺曦 2024-12-23 02:21:22
  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);
        }
    }
  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;

        }

//Request Multiple Permissions

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);
        }
    }
掌心的温暖 2024-12-23 02:21:22
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();
        }
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();
        }
演出会有结束 2024-12-23 02:21:22
//gets path of the directory("imRec") in Android's data folder
File file=new File(getApplicationContext().getExternalFilesDir("imRec").getAbsolutePath());

if (!file.exists()) {  //if directory by that name doesn't exist
    if (!file.mkdirs()) { //we create the directory
          //if still directory doesn't get created ERROR mssg gets displayed
          //which in most cases won't happen
          Toast.makeText(CameraActivity.this, "ERROR", Toast.LENGTH_SHORT).show();          
    }
}
//gets path of the directory("imRec") in Android's data folder
File file=new File(getApplicationContext().getExternalFilesDir("imRec").getAbsolutePath());

if (!file.exists()) {  //if directory by that name doesn't exist
    if (!file.mkdirs()) { //we create the directory
          //if still directory doesn't get created ERROR mssg gets displayed
          //which in most cases won't happen
          Toast.makeText(CameraActivity.this, "ERROR", Toast.LENGTH_SHORT).show();          
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文