[英]How to download and save file from Google Cloud Storage on android?
[英]Save file to cloud storage and modify from different devices on Android
我希望我的應用程序允許用戶將文件保存到 Google Drive 等雲存儲服務,並從不同的設備打開和編輯該文件。
我嘗試使用此處解釋的存儲訪問框架,但存在以下問題:
當設備 B 上的應用程序打開設備 A 上的應用程序創建的文件時,似乎創建了該文件的新版本。
即在設備B上修改文件並保存到Google Drive(使用打開文件時獲取的Uri)后,在設備A上打開文件時,更改不會反映。 反之亦然。
Dropbox 顯示了類似的行為,不同之處在於 Dropbox 應用程序中顯示了不同的版本,而在 Drive 應用程序和 Web 界面中,只顯示了一個版本的文件。
如何解決這個問題?
這是存儲訪問框架中固有的,還是可能有另一個雲存儲提供商不會出現此問題?
我擔心唯一的解決方案是直接使用 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()
}
}
}
我已經有了部分解決方案。 當我發現更多信息時,我會更新這個答案。
使用 box ( https://www.box.com ) 實際上不會出現問題,因此它不是存儲訪問框架中固有的。
編輯: Box似乎是唯一一個流行的雲提供商,通過存儲訪問框架從不同設備修改文件沒有這個問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.