简体   繁体   中英

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.

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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