如何更改此代码,以使其打开画廊并选择照片。我不需要相机功能。谢谢
我的应用程序中需要此功能,但是我无法更改该部分代码。有人可以帮助我适应它。
我用您指示的修改内容更新了帖子。
编辑
错误行
val bitmap = MediaStore.Images.Media.getBitmap(context.contentResolver, this.imageUri) as Bitmap
无法交付结果ResultInfo {who = android:fragment:2, request = 1000,结果= -1,data =意图{ dat = content://com.google.android.apps.photos.contentprovider/-1/1/content:// media / external / images / media / 48 / ORIGINAL / NONE / 207210023 flg = 0x1剪辑= {文本/ uri-list U:content://com.google.android.apps.photos.contentprovider/-1/1/content%3A%2F%2Fmedia%2Fexternal%2Fimages%2Fmedia%2F48/ORIGINAL/NONE/207210023} }}活动{maps.com/maps.com.MainActivity}: java.lang.NullPointerException:uri
private fun checkPermissionForImage() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if ((checkSelfPermission(this.context, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED)
&& (checkSelfPermission(this.context, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED)
) {
val permission = arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE)
val permissionCoarse = arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE)
requestPermissions(permission, PERMISSION_CODE_READ) // GIVE AN INTEGER VALUE FOR PERMISSION_CODE_READ LIKE 1001
requestPermissions(permissionCoarse, PERMISSION_CODE_WRITE) // GIVE AN INTEGER VALUE FOR PERMISSION_CODE_WRITE LIKE 1002
} else {
pickImageFromGallery()
}
}
}
private fun pickImageFromGallery() {
val intent = Intent(Intent.ACTION_PICK)
intent.type = "image/*"
startActivityForResult(intent, IMAGE_PICK_CODE) // GIVE AN INTEGER VALUE FOR IMAGE_PICK_CODE LIKE 1000
}
/**
* Save the picture to the thumbnail after taking it
*/
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == IMAGE_PICK_CODE && resultCode == RESULT_OK) {
try {
//Getting the Bitmap from Gallery
val bitmap = MediaStore.Images.Media.getBitmap(context.contentResolver, this.imageUri) as Bitmap
this.imgThumb!!.setImageBitmap(bitmap)
this.pictureTaken = true
} catch (e:IOException) {
e.printStackTrace()
}
} else {
Toast.makeText(context, "Error loading image", Toast.LENGTH_LONG)
}
}
@Throws(IOException::class)
fun createImageFile(): File {
val timeStamp: String = SimpleDateFormat("yyyyMMdd_HHmmss").format(Date())
val imageFileName: String = "JPEG_" + timeStamp + "_"
val storageDir: File = activity.getExternalFilesDir(Environment.DIRECTORY_PICTURES)
if(!storageDir.exists()) storageDir.mkdirs()
val imageFile = File.createTempFile(imageFileName, ".jpg", storageDir)
imageFilePath = imageFile.absolutePath
return imageFile
}
答案 0 :(得分:1)
首先,您必须检查存储权限:
private fun checkPermissionForImage() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if ((checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED)
&& (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED)
) {
val permission = arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE)
val permissionCoarse = arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE)
requestPermissions(permission, PERMISSION_CODE_READ) // GIVE AN INTEGER VALUE FOR PERMISSION_CODE_READ LIKE 1001
requestPermissions(permissionCoarse, PERMISSION_CODE_WRITE) // GIVE AN INTEGER VALUE FOR PERMISSION_CODE_WRITE LIKE 1002
} else {
pickImageFromGallery()
}
}
}
如您所见,如果授予了权限,则我正在访问名为pickImageFromGallery()
的方法。这是为了访问图库以选择图像:
private fun pickImageFromGallery() {
val intent = Intent(Intent.ACTION_PICK)
intent.type = "image/*"
startActivityForResult(intent, IMAGE_PICK_CODE) // GIVE AN INTEGER VALUE FOR IMAGE_PICK_CODE LIKE 1000
}
现在,您必须重写onActivityResult
方法。这将返回您选择的图像URI
。
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == Activity.RESULT_OK && requestCode == IMAGE_PICK_CODE) {
// I'M GETTING THE URI OF THE IMAGE AS DATA AND SETTING IT TO THE IMAGEVIEW
imageView.setImageURI(data?.data)
}
}