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

发布于 2025-02-06 05:26:54 字数 1863 浏览 1 评论 0原文

我希望我的应用程序让用户将文件保存到Google Drive之类的云存储服务中,并从不同设备打开并编辑该文件。

我尝试使用“存储访问框架”,如说明在这里 ,但这存在以下问题:

当设备B上的应用程序打开该应用程序在设备A上创建的文件时,似乎创建了该文件的新版本。

即,在将文件在设备B上修改并保存到Google驱动器(使用打开文件时获得的URI)之后,当文件在设备A上打开时,则不会反映更改。相同的恶习。

Dropbox显示了类似的行为,并且在Dropbox应用中显示了不同版本的区别,而在Drive应用程序和Web接口中,仅出现一个版本的文件。

如何解决这个问题?

  1. 是存储访问框架中固有的,或者可能 还有另一个没有发生此问题的云存储提供商?

  2. 我担心唯一的解决方案是直接使用Google Drive API (这会阻止用户使用其他服务),或者有更简单,更一般的解决方案?

这里我尝试了简化的代码:

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

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.

I.e., 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. 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.

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?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小忆控 2025-02-13 05:26:54

我已经有部分解决方案。找到更多信息时,我将更新此答案。

使用框( https://www.box.com )实际上没有发生问题存储访问框架固有的。

编辑: box 似乎是唯一一个流行的云提供商,可以通过存储访问框架从不同设备上修改文件而没有该问题。

  • OneDrive,Amazon Drive,Yandex Disk,Mega,MediaFire 似乎没有为存储访问框架注册(不要在Save-Dialog中显示)
  • Google drive,dropbox,pcloud < /strong>从另一个设备访问文件时创建新版本

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.

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.

  • 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)
  • Google Drive, Dropbox, pCloud create new versions when accessing the file from another device
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文