简体   繁体   English

将文件保存到云存储并从 Android 上的不同设备进行修改

[英]Save file to cloud storage and modify from different devices on Android

I'd like my app to let the user save a file to a cloud storage service like Google Drive, and open and edit that file from different devices.我希望我的应用程序允许用户将文件保存到 Google Drive 等云存储服务,并从不同的设备打开和编辑该文件。

I tried to use the the Storage Access Framework as explained here , but that has the following problem:我尝试使用此处解释的存储访问框架,但存在以下问题:

When the app on device B opens a file that was created by the app on device A, a new version of the file seems to be created.当设备 B 上的应用程序打开设备 A 上的应用程序创建的文件时,似乎创建了该文件的新版本。

Ie, after the file is modified on device B and saved to Google Drive (using the Uri obtained when opening the file), when the file is opened on device A, the changes are not reflected.即在设备B上修改文件并保存到Google Drive(使用打开文件时获取的Uri)后,在设备A上打开文件时,更改不会反映。 The same vice versa.反之亦然。

Dropbox shows a similar behaviour, with the difference that the different versions show up in the Dropbox app, while in the Drive app and web interface, only one version of the file appears. Dropbox 显示了类似的行为,不同之处在于 Dropbox 应用程序中显示了不同的版本,而在 Drive 应用程序和 Web 界面中,只显示了一个版本的文件。

How to solve this?如何解决这个问题?

  1. Is this inherent in the Storage Access Framework, or may there be another cloud storage provider where this problem does not occur?这是存储访问框架中固有的,还是可能有另一个云存储提供商不会出现此问题?

  2. I fear the only solution is to use the Google Drive API directly (which would prevent users from using other services), or is there a simpler, more general solution?我担心唯一的解决方案是直接使用 Google Drive API(这会阻止用户使用其他服务),还是有更简单、更通用的解决方案?

Here the simplified code I tried:这是我尝试的简化代码:

var uri: Uri? = null

fun save() {
   if (uri == null) {
      val intent = Intent(Intent.ACTION_CREATE_DOCUMENT).apply {
         addCategory(Intent.CATEGORY_OPENABLE)
         type = "text/plain"
         putExtra(Intent.EXTRA_TITLE, "test.txt")
      }
      startActivityForResult(intent, SAVE)
   }
   else
      write()
}

fun open() {
   if (uri == null) {
      val intent = Intent(Intent.ACTION_OPEN_DOCUMENT).apply {
         addCategory(Intent.CATEGORY_OPENABLE)
         type = "text/plain"
      }
      startActivityForResult(intent, OPEN)
   }
   else
      read()
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
   if (requestCode == SAVE) {
      uri = data?.data
      write()
   }
   else if (requestCode == OPEN) {
      uri = data?.data
      read()
   }
}

fun write() {
   contentResolver.openFileDescriptor(uri!!, "w")?.use {
      FileOutputStream(it.fileDescriptor).use {
         it.channel.truncate(0)
         it.write(string_to_save.toByteArray())
      }
   }
}

fun read() {
   contentResolver.openInputStream(uri!!)?.use {
      BufferedReader(InputStreamReader(it)).use {
         string_from_file = it.readLine()
      }
   }
}

I already have a partial solution.我已经有了部分解决方案。 I will update this answer when I find out more.当我发现更多信息时,我会更新这个答案。

With box ( https://www.box.com ) the problem actually does not occur, so it is not inherent in the Storage Access Framework.使用 box ( https://www.box.com ) 实际上不会出现问题,因此它不是存储访问框架中固有的。

Edit: Box seems to be the only popular cloud provider where modifying a file from different devices via the Storage Access Framework works without that problem.编辑: Box似乎是唯一一个流行的云提供商,通过存储访问框架从不同设备修改文件没有这个问题。

  • OneDrive, Amazon Drive, Yandex Disk, Mega, MediaFire seem not to register themselves for the Storage Access Framework (don't show up in the save-dialog) OneDrive、Amazon Drive、Yandex Disk、Mega、MediaFire似乎没有为存储访问框架注册自己(不显示在保存对话框中)
  • Google Drive, Dropbox, pCloud create new versions when accessing the file from another device从其他设备访问文件时,Google Drive、Dropbox、pCloud创建新版本

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

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