我有一个问题,当从手机图库中选择个人资料图片时,它会旋转90度。即使我关闭然后重新打开应用程序,该问题仍然存在。此外,个人资料图片的尺寸要小得多,我似乎无法增加其尺寸。如果有增加大小的方法,那就太好了。任何帮助表示赞赏。
btn = findViewById<View>(R.id.btn_profile) as Button
imageview = findViewById<View>(R.id.profile_image) as ImageView
val wallpaperDirectory = File(getExternalFilesDir(null).toString() + IMAGE_DIRECTORY)
var listImages : Array<File>? = null
listImages = wallpaperDirectory.listFiles()
if(listImages != null && listImages.size!! > 0){
val img2 = BitmapFactory.decodeFile(listImages[0].absolutePath)
val round = RoundedBitmapDrawableFactory.create(resources, img2)
round.isCircular = true
profile_image.setImageDrawable(round)
}
else {
val img = BitmapFactory.decodeResource(resources, R.mipmap.profile_image)
val round = RoundedBitmapDrawableFactory.create(resources, img)
round.isCircular = true
profile_image.setImageDrawable(round)
println("hit hit hit")
}
// Profile Image
private fun showPictureDialog() {
val pictureDialog = AlertDialog.Builder(this)
pictureDialog.setTitle("Select Action")
val pictureDialogItems = arrayOf("Select Photo From Gallery", "Capture Photo From Camera")
pictureDialog.setItems(pictureDialogItems
) { dialog, which ->
when (which) {
0 -> choosePhotoFromGallary()
1 -> takePhotoFromCamera()
}
}
pictureDialog.show()
}
fun choosePhotoFromGallary() {
val galleryIntent = Intent(Intent.ACTION_PICK,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
startActivityForResult(galleryIntent, GALLERY)
}
fun takePhotoFromCamera() {
val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
startActivityForResult(intent, CAMERA)
}
public override fun onActivityResult(requestCode:Int, resultCode:Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (Activity.RESULT_OK == resultCode && requestCode == GALLERY)
{
if (data != null)
{
val contentURI = data.data
try
{
val bitmap = MediaStore.Images.Media.getBitmap(this.contentResolver, contentURI)
val round = RoundedBitmapDrawableFactory.create(resources, bitmap)
round.isCircular = true
val path = saveImage(bitmap)
Toast.makeText(this@MainActivity, "Image Saved!", Toast.LENGTH_SHORT).show()
imageview!!.setImageDrawable(round)
}
catch (e: IOException) {
e.printStackTrace()
Toast.makeText(this@MainActivity, "Failed!", Toast.LENGTH_SHORT).show()
}
}
}
else if (Activity.RESULT_OK == resultCode && requestCode == CAMERA)
{
val thumbnail = data!!.extras!!.get("data") as Bitmap
val round = RoundedBitmapDrawableFactory.create(resources, thumbnail)
round.isCircular = true
imageview!!.setImageDrawable(round)
saveImage(thumbnail)
Toast.makeText(this@MainActivity, "Image Saved!", Toast.LENGTH_SHORT).show()
}
}
fun saveImage(myBitmap: Bitmap):String {
val bytes = ByteArrayOutputStream()
myBitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes)
val wallpaperDirectory = File(getExternalFilesDir(null).toString() + IMAGE_DIRECTORY)
wallpaperDirectory.deleteRecursively()
// have the object build the directory structure, if needed.
Log.d("fee",wallpaperDirectory.toString())
if (!wallpaperDirectory.exists())
{
wallpaperDirectory.mkdirs()
}
try
{
Log.d("heel",wallpaperDirectory.toString())
val f = File(wallpaperDirectory, ((Calendar.getInstance()
.getTimeInMillis()).toString() + ".jpg"))
f.createNewFile()
val fo = FileOutputStream(f)
fo.write(bytes.toByteArray())
MediaScannerConnection.scanFile(this,
arrayOf(f.getPath()),
arrayOf("image/jpeg"), null)
fo.close()
Log.d("TAG", "File Saved::--->" + f.getAbsolutePath())
return f.getAbsolutePath()
}
catch (e1: IOException) {
e1.printStackTrace()
}
return ""
}
companion object {
private val IMAGE_DIRECTORY = "/demonuts"
}
// Profile Image
我想出来了,我忘了在var位图旋转后放入val rotatedBitmap = bitmap.rotate(90F)
,