检索实际文件而不是 URI 原始位置
Retrieving actual File instead of URI original location
基于 the docs used for configuring your app as a platform for sharing media files, how do I store an actual File as opposed to its orginal URI location using a FileProvider 即使我已经从 Intent 获得了一个 URI?例如,如果我想要 "content://com.android.sample.fileprovider/my_images/JPEG_20180315_162048_-1397281749.jpg" 的 URI 字符串。因为截至目前,我使用以下代码得到 "content://media/external/images/media/142607":
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_media)
val intent = intent
val action = intent.action
val type = intent.type
type?.let {
when (action) {
Intent.ACTION_SEND -> {
if (it == getString(R.string.mime_type_text)) {
handleSendText(intent)
} else if (it.startsWith(getString(R.string.mime_type_image_any))) {
handleSendImage(intent)
}
}
}
}
private fun handleSendImage(intent: Intent?) {
val imageUri = intent?.getParcelableExtra<Uri>(Intent.EXTRA_STREAM)
imageUri?.let {
// Logs "content://media/external/images/media/142607"
Log.d("tag", it.toString())
// Logs "/external/images/media/142607"
Log.d("tag", File(it.path).toString())
}
}
how do I store an actual File as opposed to its orginal URI location
第 1 步:在您的 Activity
上调用 getContentResolver()
,以获得 ContentResolver
步骤 #2:在 ContentResolver
上调用 openInputStream()
,以在 Uri
标识的内容上获得 InputStream
第 3 步:将字节从 InputStream
复制到某些内容(例如,您控制的文件上的 FileOutputStream
)
第 4 步:完成所有工作后,将其移至后台线程,这样您的 I/O 就不会占用主应用程序线程
For instance, if I wanted a URI string of "content://com.android.sample.fileprovider/my_images/JPEG_20180315_162048_-1397281749.jpg"
首先,没有要求每个 Android 应用程序的每个开发人员都使用 FileProvider
通过 ACTION_SEND
向您发送流。
其次,您很可能没有文件系统访问权限。
基于 the docs used for configuring your app as a platform for sharing media files, how do I store an actual File as opposed to its orginal URI location using a FileProvider 即使我已经从 Intent 获得了一个 URI?例如,如果我想要 "content://com.android.sample.fileprovider/my_images/JPEG_20180315_162048_-1397281749.jpg" 的 URI 字符串。因为截至目前,我使用以下代码得到 "content://media/external/images/media/142607":
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_media)
val intent = intent
val action = intent.action
val type = intent.type
type?.let {
when (action) {
Intent.ACTION_SEND -> {
if (it == getString(R.string.mime_type_text)) {
handleSendText(intent)
} else if (it.startsWith(getString(R.string.mime_type_image_any))) {
handleSendImage(intent)
}
}
}
}
private fun handleSendImage(intent: Intent?) {
val imageUri = intent?.getParcelableExtra<Uri>(Intent.EXTRA_STREAM)
imageUri?.let {
// Logs "content://media/external/images/media/142607"
Log.d("tag", it.toString())
// Logs "/external/images/media/142607"
Log.d("tag", File(it.path).toString())
}
}
how do I store an actual File as opposed to its orginal URI location
第 1 步:在您的 Activity
上调用 getContentResolver()
,以获得 ContentResolver
步骤 #2:在 ContentResolver
上调用 openInputStream()
,以在 Uri
InputStream
第 3 步:将字节从 InputStream
复制到某些内容(例如,您控制的文件上的 FileOutputStream
)
第 4 步:完成所有工作后,将其移至后台线程,这样您的 I/O 就不会占用主应用程序线程
For instance, if I wanted a URI string of "content://com.android.sample.fileprovider/my_images/JPEG_20180315_162048_-1397281749.jpg"
首先,没有要求每个 Android 应用程序的每个开发人员都使用 FileProvider
通过 ACTION_SEND
向您发送流。
其次,您很可能没有文件系统访问权限。