来自画廊的照片旋转 90 度和小问题
Posted
技术标签:
【中文标题】来自画廊的照片旋转 90 度和小问题【英文标题】:Issue With Photo from Gallery Rotated 90 Degrees and Small 【发布时间】:2020-09-06 06:30:44 【问题描述】:我有一个问题,当从手机的图库中选择个人资料图片时,它会旋转 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
【问题讨论】:
【参考方案1】:我想通了——我忘了在 var bitmap 之后输入val rotatedBitmap = bitmap.rotate(90F)
来旋转它
【讨论】:
以上是关于来自画廊的照片旋转 90 度和小问题的主要内容,如果未能解决你的问题,请参考以下文章
ios系统 竖屏拍照 canvas处理后 图片旋转(利用exif.js解决ios手机上传竖拍照片旋转90度问题)
利用exif.js解决ios或Android手机上传竖拍照片旋转90度问题