[英]Append data to text file in android 10 (API 29)
I am working on an application where app writes a log file with the currentdate as filename我正在开发一个应用程序,其中应用程序将当前日期作为文件名写入日志文件
Ex: 20200710.txt例如:20200710.txt
The earlier was working fine before android 10 but from android 10 the code is no longer writing the file in the external storage.较早的在 android 10 之前运行良好,但从 android 10 开始,代码不再将文件写入外部存储。
So I have modified the code a bit for android 10 especially所以我特别为android 10修改了代码
string logDir = "Documents/MyApp_Data/logs/";
Context context = MyApplication.Context;
ContentValues values = new ContentValues();
values.Put(MediaStore.MediaColumns.DisplayName, filename);
values.Put(MediaStore.MediaColumns.MimeType, "text/plain"); //file extension, will automatically add to file
values.Put(MediaStore.MediaColumns.RelativePath, logDir);
var uri = context.ContentResolver.Insert(MediaStore.Files.GetContentUri("external"), values);
Stream outputStream = context.ContentResolver.OpenOutputStream(uri, "rw");
outputStream.Write(Encoding.UTF8.GetBytes(message));
outputStream.Close();
The above code is working for android 10 but it is creating multiple log files instead I want to update the file if the file already exists.上面的代码适用于 android 10,但它正在创建多个日志文件,如果文件已经存在,我想更新文件。 I am not getting a way to check if the file exists then append new data in the existing file.我没有办法检查文件是否存在,然后在现有文件中附加新数据。 Can someone please let me know?有人可以让我知道吗? The above code is in Xamarin android but if you have any suggestion that will work in android then I will convert that code to Xamarin android上面的代码是在 Xamarin android 中,但如果你有任何建议可以在 android 中工作,那么我会将该代码转换为 Xamarin android
Thanks in advance提前致谢
This code corrects (especially words' upper/lower cases) vaibhav ones and use blackapps suggestion to include text append.此代码更正(尤其是单词的大写/小写)vaibhav 并使用 blackapps 建议包含文本附加。 Can write txt or json.可以写txt或json。 Good to write text in persistent folders (eg /storage/self/Downloads) without user interaction on Android 10+ (actually not tested on 11, but should work).在 Android 10+ 上无需用户交互即可在持久文件夹(例如 /storage/self/Downloads)中写入文本(实际上未在 11 上测试,但应该可以工作)。
// filename can be a String for a new file, or an Uri to append it
fun saveTextQ(ctx: Context,
relpathOrUri: Any,
text: String,
dir: String = Environment.DIRECTORY_DOWNLOADS):Uri?{
val fileUri = when (relpathOrUri) {
is String -> {
// create new file
val mime = if (relpathOrUri.endsWith("json")) "application/json"
else "text/plain"
val values = ContentValues()
values.put(MediaStore.MediaColumns.DISPLAY_NAME, relpathOrUri)
values.put(MediaStore.MediaColumns.MIME_TYPE, mime) //file extension, will automatically add to file
values.put(MediaStore.MediaColumns.RELATIVE_PATH, dir)
ctx.contentResolver.insert(MediaStore.Files.getContentUri("external"), values) ?: return null
}
is Uri -> relpathOrUri // use given Uri to append existing file
else -> return null
}
val outputStream = ctx.contentResolver.openOutputStream(fileUri, "wa") ?: return null
outputStream.write(text.toByteArray(charset("UTF-8")))
outputStream.close()
return fileUri // return Uri to then allow append
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.