android内部存储目录的绝对路径?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25905649/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:48:55  来源:igfitidea点击:

absolute path to directory in the internal storage in android?

androiddirectoryinternal-storage

提问by Blue_Alien

I want to keep my app required files inside a directory under internal storage. in the previous stage i used externalStorageDirectory to store my data. now i am using the below code to refer my external directory

我想将我的应用程序所需的文件保存在内部存储下的目录中。在前一阶段,我使用 externalStorageDirectory 来存储我的数据。现在我使用下面的代码来引用我的外部目录

 Environment.getExternalStorageDirectory()+"/mydirectory"; 

I need an equivalant code to refer my directory in the internal directory, can anybody help me...thanks,

我需要一个等效的代码来引用我在内部目录中的目录,任何人都可以帮助我...谢谢,

Solution:

解决方案:

getFilesDir()+"/mydirectory";

thanks for all of your suggestion and help.

感谢您的所有建议和帮助。

回答by Simas

Use getFilesDir()for that.

getFilesDir()为此使用。

From the docs:

文档

Returns the absolute path to the directory on the filesystem where files created with openFileOutput(String, int) are stored.

No permissions are required to read or write to the returned path, since this path is internal storage.

返回文件系统上存储使用 openFileOutput(String, int) 创建的文件的目录的绝对路径。

读取或写入返回的路径不需要任何权限,因为此路径是内部存储。

To answer your comment, I'll quote the docs docs:

为了回答您的评论,我将引用 docs docs

By default, files saved to the internal storage are private to your application and other applications cannot access them (nor can the user). When the user uninstalls your application, these files are removed.

默认情况下,保存到内部存储的文件对您的应用程序是私有的,其他应用程序无法访问它们(用户也不能)。当用户卸载您的应用程序时,这些文件将被删除。

回答by Sainath Patwary karnate

public  File createDir() {
         FIle DIR=null;
         if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
                DIR=new File(android.os.Environment.getExternalStorageDirectory()+DIRNAME);
            else
                DIR=context.getCacheDir();
            if(!DIR.exists())
                DIR.mkdirs();
            return DIR;  
    }

This one i m using in one of my application and which creates the directory in external if it is mounted or not exists it will create in internal storage

我在我的一个应用程序中使用了这个,如果它已安装或不存在,它将在外部创建目录,它将在内部存储中创建

回答by Harsh Parikh

You can use this:

你可以使用这个:

File f = new File(Environment.getDataDirectory(), "subdir");