[英]Persistable permissions URI android Q
I'm using the emulator and I send the intent in this way:我正在使用模拟器并以这种方式发送意图:
StorageManager sm = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
Intent i;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
i = sm.getPrimaryStorageVolume().createOpenDocumentTreeIntent();
} else {
return null;
}
i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
i.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
i.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
and in onActivityResult()
:在onActivityResult()
:
Uri uri = data.getData();
if (uri == null)
return null;
context.getContentResolver()
.takePersistableUriPermission(uri,
Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
Every time I reboot, the app doesn't have access.每次我重新启动时,该应用程序都无权访问。 Can anyone confirm about this bug on beta 6 on the emulator?任何人都可以在模拟器上的 beta 6 上确认这个错误吗? Am I doing anything wrong?我做错了什么吗?
I converted your code snippets into this Kotlin activity:我将您的代码片段转换为此 Kotlin 活动:
package com.commonsware.jetpack.myapplication
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.net.Uri
import android.os.Bundle
import android.os.storage.StorageManager
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.documentfile.provider.DocumentFile
private const val PREF_URI = "uri"
private const val REQUEST_SAF = 1337
class MainActivity : AppCompatActivity() {
private val prefs: SharedPreferences by lazy {
getSharedPreferences("test", Context.MODE_PRIVATE)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val uriString = prefs.getString(PREF_URI, null)
val storageManager = getSystemService(StorageManager::class.java)!!
if (uriString == null) {
startActivityForResult(
storageManager.primaryStorageVolume.createOpenDocumentTreeIntent(),
REQUEST_SAF
)
} else {
val uri = Uri.parse(uriString)
val docFile = DocumentFile.fromTreeUri(this, uri)
Toast.makeText(
this,
"canRead: ${docFile?.canRead()} canWrite: ${docFile?.canWrite()}",
Toast.LENGTH_LONG
).show()
}
}
override fun onActivityResult(
requestCode: Int,
resultCode: Int,
data: Intent?
) {
val uri = data?.data
if (uri == null) {
Toast.makeText(this, "Did not get a Uri??!?", Toast.LENGTH_LONG).show()
} else {
contentResolver.takePersistableUriPermission(
uri,
Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
)
prefs.edit().putString(PREF_URI, uri.toString()).apply()
Toast.makeText(this, "OK, run the activity again", Toast.LENGTH_LONG).show()
}
finish()
}
}
I then ran the app containing this activity as its launcher activity and chose Downloads/
as the document tree.然后我运行包含此活动的应用程序作为其启动器活动,并选择Downloads/
作为文档树。 Subsequent launches of this activity — both before and after a reboot — show canRead()
and canWrite()
both return true
.此活动的后续启动 - 在重新启动之前和之后 - 显示canRead()
和canWrite()
都返回true
。 This was tested on a Google Pixel running Q Beta 6.这是在运行 Q Beta 6 的 Google Pixel 上进行测试的。
I cannot try this on a Q emulator because Google , so I cannot confirm if I can reproduce your behavior there.由于 Google ,我无法在 Q 模拟器上尝试此操作,因此我无法确认是否可以在那里重现您的行为。 But, the fact that this works as expected on hardware should be a positive sign.但是,这在硬件上按预期工作的事实应该是一个积极的信号。
For those who appreciate the accepted answer, but do not want to use startActivityForResult
, which was deprecated after the original answer was accepted.对于那些欣赏已接受答案但不想使用startActivityForResult
,在接受原始答案后已弃用。
My example shows how to save a file to storage from within your app.我的示例展示了如何从您的应用程序中将文件保存到存储中。
First, create a class-wide variable to hold the ActivityResultLauncher
information:首先,创建一个类范围的变量来保存ActivityResultLauncher
信息:
private lateinit var saveDataResultLauncher: ActivityResultLauncher<String>
Then, instead of using the deprecated feature, you can put this, for example, in your MainActivity
's onCreate
fun:然后,您可以将其放在MainActivity
的onCreate
fun 中,而不是使用已弃用的功能:
/** Backup Website Entries */
saveDataResultLauncher = registerForActivityResult(ActivityResultContracts.CreateDocument()) {}
Make the user choose a location to save a file, after pressing a menu item bound to the functionality implemented above:在按下与上面实现的功能绑定的菜单项后,让用户选择一个位置来保存文件:
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
R.id.action_save_file -> {
saveDataResultLauncher.launch("example.txt")
true
}
else -> super.onOptionsItemSelected(item)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.