将数据附加到 android 10 (API 29) 中的文本文件 [英] Append data to text file in android 10 (API 29)

查看:25
本文介绍了将数据附加到 android 10 (API 29) 中的文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,其中应用程序将当前日期作为文件名写入日志文件

I am working on an application where app writes a log file with the currentdate as filename

例如:20200710.txt

Ex: 20200710.txt

较早版本在 android 10 之前运行良好,但从 android 10 开始,代码不再将文件写入外部存储.

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 修改了代码

So I have modified the code a bit for android 10 especially

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

上述代码适用于 android 10,但它正在创建多个日志文件,如果文件已存在,我想更新该文件.我没有办法检查文件是否存在,然后在现有文件中附加新数据.有人可以让我知道吗?上面的代码是在 Xamarin android 中,但如果你有任何建议可以在 android 中工作,那么我会将该代码转换为 Xamarin android

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

提前致谢

推荐答案

此代码更正(尤其是单词的大写/小写)vaibhav 并使用 blackapps 建议包含文本附加.可以写txt或json.在 Android 10+ 上无需用户交互即可在持久文件夹(例如/storage/self/Downloads)中写入文本(实际上未在 11 上测试,但应该可以工作).

This code corrects (especially words' upper/lower cases) vaibhav ones and use blackapps suggestion to include text append. Can write txt or json. Good to write text in persistent folders (e.g. /storage/self/Downloads) without user interaction on Android 10+ (actually not tested on 11, but should work).

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

这篇关于将数据附加到 android 10 (API 29) 中的文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆