无法从 Google 照片应用中选取图像
Unable to pick image from Google Photos app
我正在使用以下代码(在 Kotlin 中)select 来自三星平板电脑上 Google 照片应用的图像。
val intent = Intent (Intent.ACTION_GET_CONTENT)
intent.type = "image/*"
startActivityForResult(intent, REQUEST_GOOGLE_PHOTOS_IMAGE)
我也试过了
Intent (Intent.ACTION_GET_CONTENT,MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
在 运行 这段代码中,我得到一个滑出栏,允许我 select 照片应用程序,然后我可以从中 select 一张照片。但是,一旦我 select 照片,应用程序就不会 return 图像到我的应用程序,就像它对相机和图库所做的那样。它改为 returns 滑出。当我点击后退按钮关闭滑出时,onActivityResult 被调用 RESULT_CANCELED 和 0 数据。
我可以毫无问题地从图库和相机中检索照片,所以我不确定我遗漏了什么。也许是清单中的许可或其他内容?提前致谢!
让我们这样试试,
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
} else {
intent = new Intent(Intent.ACTION_GET_CONTENT);
}
使用 ACTION_GET_CONTENT 意图操作 - Intent.ACTION_GET_CONTENT
调用下面的selectImage()
方法开始pick image Intent,会显示select image from
可用的所有应用
private val RC_SELECT_IMGAE = 101
private fun selectImage() {
val selectImageIntent = Intent(Intent.ACTION_GET_CONTENT, MediaStore.Images.Media
.EXTERNAL_CONTENT_URI)
startActivityForResult(selectImageIntent, RC_SELECT_IMGAE)
}
然后通过覆盖 Activity
中的 onActivityResult()
方法来处理回调
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
when (requestCode) {
RC_SELECT_IMGAE -> {
if (data != null) {
val uri = data.data
displaySelectedImage(getBitmapFromUri(uri))
}
}
else -> super.onActivityResult(requestCode, resultCode, data)
}
}
最后用位图更新图像视图如下
private fun getBitmapFromUri(uri: Uri): Bitmap {
val parcelFileDescriptor = contentResolver.openFileDescriptor(uri, "r")
val fileDescriptor = parcelFileDescriptor?.fileDescriptor
val image = BitmapFactory.decodeFileDescriptor(fileDescriptor)
parcelFileDescriptor.close()
return image
}
private fun displaySelectedImage(imageBitmap: Bitmap) {
iv_selected_image.setImageBitmap(imageBitmap)
}
在此处查找工作图像选择器示例 - ImagePickerExample
您可能需要添加 FileProvider
以防它给出任何类型的 URI 异常
- Android Documentation - Setup Sharing
- Setup File Provider
问题是我没有连接到网络,所以 Google 照片无法检索所选图像。请参阅我对@adityakamble49 的回复。
我正在使用以下代码(在 Kotlin 中)select 来自三星平板电脑上 Google 照片应用的图像。
val intent = Intent (Intent.ACTION_GET_CONTENT)
intent.type = "image/*"
startActivityForResult(intent, REQUEST_GOOGLE_PHOTOS_IMAGE)
我也试过了
Intent (Intent.ACTION_GET_CONTENT,MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
在 运行 这段代码中,我得到一个滑出栏,允许我 select 照片应用程序,然后我可以从中 select 一张照片。但是,一旦我 select 照片,应用程序就不会 return 图像到我的应用程序,就像它对相机和图库所做的那样。它改为 returns 滑出。当我点击后退按钮关闭滑出时,onActivityResult 被调用 RESULT_CANCELED 和 0 数据。
我可以毫无问题地从图库和相机中检索照片,所以我不确定我遗漏了什么。也许是清单中的许可或其他内容?提前致谢!
让我们这样试试,
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
} else {
intent = new Intent(Intent.ACTION_GET_CONTENT);
}
使用 ACTION_GET_CONTENT 意图操作 - Intent.ACTION_GET_CONTENT
调用下面的selectImage()
方法开始pick image Intent,会显示select image from
private val RC_SELECT_IMGAE = 101
private fun selectImage() {
val selectImageIntent = Intent(Intent.ACTION_GET_CONTENT, MediaStore.Images.Media
.EXTERNAL_CONTENT_URI)
startActivityForResult(selectImageIntent, RC_SELECT_IMGAE)
}
然后通过覆盖 Activity
中的onActivityResult()
方法来处理回调
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
when (requestCode) {
RC_SELECT_IMGAE -> {
if (data != null) {
val uri = data.data
displaySelectedImage(getBitmapFromUri(uri))
}
}
else -> super.onActivityResult(requestCode, resultCode, data)
}
}
最后用位图更新图像视图如下
private fun getBitmapFromUri(uri: Uri): Bitmap {
val parcelFileDescriptor = contentResolver.openFileDescriptor(uri, "r")
val fileDescriptor = parcelFileDescriptor?.fileDescriptor
val image = BitmapFactory.decodeFileDescriptor(fileDescriptor)
parcelFileDescriptor.close()
return image
}
private fun displaySelectedImage(imageBitmap: Bitmap) {
iv_selected_image.setImageBitmap(imageBitmap)
}
在此处查找工作图像选择器示例 - ImagePickerExample
您可能需要添加 FileProvider
以防它给出任何类型的 URI 异常
- Android Documentation - Setup Sharing
- Setup File Provider
问题是我没有连接到网络,所以 Google 照片无法检索所选图像。请参阅我对@adityakamble49 的回复。