简体   繁体   English

Android,文件夹删除一段时间后

[英]Android, folder deleted after some time

My app is creating a folder and then an xml file on the sdcard. 我的应用程序正在创建一个文件夹,然后在SD卡上创建一个xml文件。 But after some time the folder is deleted and the file with it. 但是一段时间后文件夹被删除,文件被删除了。 The file is used to store a backup of the data from the app. 该文件用于存储来自应用程序的数据备份。

I dont need something new to make backup but a description of why it happens. 我不需要新的东西来进行备份,而是描述它为什么会发生。 The folder is created in the root of the sdcard with the name 'backup'. 该文件夹在sdcard的根目录中创建,名称为“backup”。

This is how I create the folder and file: 这是我创建文件夹和文件的方式:

File path = new File(Environment.getExternalStorageDirectory() + "/backup/tet/");
path.mkdirs();
File backupdir = new File(path, getDateTime() + ".xml");
fos = new FileOutputStream(backupdir);
fos.write(encodeDb(ctx).getBytes());
fos.close();

This is how I read the file: 这是我读取文件的方式:

boolean parsed = false;
File file = new File(Environment.getExternalStorageDirectory() + "/backup/tet/" + filename);
fis = new FileInputStream(file);
byte[] buffer = new byte[(int) file.length()];
fis.read(buffer, 0, (int) file.length());
parsed = decodeDb(ctx, new String(buffer));
fis.close();

I hope you can help me to discuss the problem. 我希望你能帮助我讨论这个问题。

Maybe some other app also uses a folder named "backup" and then deletes it? 也许其他一些应用程序也使用名为“backup”的文件夹,然后将其删除? I would name the folder differently, to make sure it's specific for your app. 我会以不同的方式命名该文件夹,以确保它特定于您的应用程序。

Making a new folder for your app isn't usually recommended, as it can result in other apps using the same folders (as Ilya suggested) as well as cluttering the SD card (and what user likes that?) Instead, it's best to use the command getExternalFilesDir(). 通常不建议为您的应用创建一个新文件夹,因为它可能会导致其他应用使用相同的文件夹(如Ilya建议的那样)以及混乱SD卡(以及用户喜欢哪种?)相反,最好使用它命令getExternalFilesDir()。 Here's what Google's developer guides have to say: 以下是Google的开发者指南所说的内容:

If you're using API Level 8 or greater, use getExternalFilesDir() to open a File that represents the external storage directory where you should save your files. 如果您使用的是API级别8或更高级别,请使用getExternalFilesDir()打开一个文件,该文件表示应保存文件的外部存储目录。 This method takes a type parameter that specifies the type of subdirectory you want, such as DIRECTORY_MUSIC and DIRECTORY_RINGTONES (pass null to receive the root of your application's file directory). 此方法采用type参数指定所需的子目录类型,例如DIRECTORY_MUSIC和DIRECTORY_RINGTONES(传递null以接收应用程序文件目录的根目录)。 This method will create the appropriate directory if necessary. 如有必要,此方法将创建适当的目录。 By specifying the type of directory, you ensure that the Android's media scanner will properly categorize your files in the system (for example, ringtones are identified as ringtones and not music). 通过指定目录类型,您可以确保Android的媒体扫描程序可以正确地对系统中的文件进行分类(例如,铃声被识别为铃声而不是音乐)。 If the user uninstalls your application, this directory and all its contents will be deleted. 如果用户卸载了您的应用程序,则将删除此目录及其所有内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM