- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 Android 的内部助手首先从系统获取我的文件的路径,然后将我的文件放在系统需要的位置。因为明天他们可能会改变主意。
我制作了一个简单的程序来探索这个主题。这是我的代码;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String path = letsMakeAfile(this, "myFile.txt");
}
private static String letsMakeAfile(Context context, String nameOfFile) {
String strOfFinalPath ="";
//ask the system what path to use...
String strOfContextPath = context.getFilesDir() + "/";
//line above doesnt work without ' + "/" ' or something on the end
//line above records this path: /data/user/0/com.example.filesexperimenting/files/
//this appears to be an invalid path unless "user" is a hidden directory
Log.d("IDIOT", "strOfContextPath: "+ strOfContextPath);
try
{
File file = new File(strOfContextPath, nameOfFile);
if (file.exists() == false) {
file.mkdirs();
//after this line "makes dirs" is file automatically still made and dropped in?
letsMakeAfile(context, nameOfFile);
//I assume not so Ive made a recursive call
}
else
;
//escape recursion....
strOfFinalPath = file.getAbsolutePath();
//Here I record the path where I hope the file is located
Log.d("IDIOT", "strOfFinalPath: "+ strOfFinalPath);
}
catch (Exception e) {
e.printStackTrace();
Log.d("IDIOT", "CATCH ERROR: "+ e.getLocalizedMessage());
}
//runs without a catch
return strOfFinalPath;
}
}
日志:
2019-04-09 09:59:22.901 16819-16819/? D/IDIOT: strOfContextPath: /data/user/0/com.example.filesexperimenting/files/
2019-04-09 09:59:22.901 16819-16819/? D/IDIOT: strOfFinalPath: /data/user/0/com.example.filesexperimenting/files
最终我从 context.getFilesDir()
得到了一个路径 /data/user/0/com.example.filesexperimenting/files/
这似乎是一个除非“用户”是一个隐藏目录,否则路径无效(那为什么我可以看到根目录?)。在 data
下的 Device File Explorer 中,唯一的其他目录是 app
、data
和 local
我错过了什么?我会假设它与 file.makedirs()
完全公开,我是一名学生,这方面的内容并不多,因此您的回复虽然以您的经验水平对您来说很明显,但应该对其他人有所帮助。我对 Java 有一些经验,对 C++ 有更多经验,但 Android 对我来说是新手。提前致谢!
最佳答案
因此,在 StackExchange 之外的谈话中,似乎像我在示例中尝试的那样使用 java.io 可能会导致一些问题,因为 Java io 可能不知道的预设文件目录可能被锁定或限制。
Android 有它自己的方法 openFileOutput(String name, int mode)
可以创建应用资源文件和它所属的目录。
从类:android.content.Context 复制的描述
操作:
~打开一个与此Context的应用程序包关联的私有(private)文件进行写入。
~如果文件不存在则创建文件。
~调用应用程序读取或写入返回的文件不需要额外的权限。
参数:
~name – 要打开的文件的名称;不能包含路径分隔符。
~mode – 操作模式。
返回:生成的 FileOutputStream。
抛出:java.io.FileNotFoundException
关于Android Studio : context. getFilesDir() 返回我找不到的路径 [/data/user/0/com.example.filesexperimenting/files/]。我错过了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55597727/
我正在尝试使用 Android 的内部助手首先从系统获取我的文件的路径,然后将我的文件放在系统需要的位置。因为明天他们可能会改变主意。 我制作了一个简单的程序来探索这个主题。这是我的代码; publi
我是一名优秀的程序员,十分优秀!