Android 图片库中的照片旋转90度且较小
我有一个问题,当从手机的多媒体资料中选择个人资料图片时,它会旋转90度。即使我关闭然后重新打开应用程序,这种情况仍然存在。此外,个人资料图片的大小要小得多,我似乎无法增加其大小。如果有办法增加尺寸,那就太好了。感谢您的帮助Android 图片库中的照片旋转90度且较小,android,android-studio,kotlin,bitmap,android-camera,Android,Android Studio,Kotlin,Bitmap,Android Camera,我有一个问题,当从手机的多媒体资料中选择个人资料图片时,它会旋转90度。即使我关闭然后重新打开应用程序,这种情况仍然存在。此外,个人资料图片的大小要小得多,我似乎无法增加其大小。如果有办法增加尺寸,那就太好了。感谢您的帮助 btn = findViewById<View>(R.id.btn_profile) as Button imageview = findViewById<View>(R.id.profile_image) as Ima
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
btn=findviewbyd(R.id.btn\u配置文件)作为按钮
imageview=findViewById(R.id.profile\u image)作为imageview
val wallperDirectory=File(getExternalFilesDir(null).toString()+IMAGE\u目录)
变量listImages:数组?=无效的
listImages=wallpdirectory.listFiles()
if(listImages!=null&&listImages.size!!>0){
val img2=BitmapFactory.decodeFile(listImages[0].absolutePath)
val round=RoundedBitmapDrawableFactory.create(参考资料,img2)
round.isCircular=true
配置文件\图像。可设置图像绘制(圆形)
}
否则{
val img=BitmapFactory.decodeResource(资源,R.mipmap.profile\u图像)
val round=RoundedBitmapDrawableFactory.create(资源,img)
round.isCircular=true
配置文件\图像。可设置图像绘制(圆形)
println(“命中”)
}
//轮廓图像
私人娱乐节目图片目录(){
val pictureDialog=AlertDialog.Builder(此)
pictureDialog.setTitle(“选择操作”)
val pictureDialogItems=arrayOf(“从图库中选择照片”、“从相机捕获照片”)
pictureDialog.setItems(pictureDialogItems
){对话框,其中->
什么时候(哪个){
0->选择Romgallary()的照片
1->从照相机拍摄照片()
}
}
pictureDialog.show()
}
有趣的选择Romgallary的照片(){
val gallerycontent=Intent(Intent.ACTION\u PICK,
MediaStore.Images.Media.EXTERNAL\u CONTENT\u URI)
startActivityForResult(画廊内容、画廊)
}
从照相机()拍摄的有趣照片{
val intent=intent(MediaStore.ACTION\u IMAGE\u捕获)
startActivityForResult(意图、摄像机)
}
在ActivityResult上的公共覆盖乐趣(请求代码:Int,结果代码:Int,数据:Intent?){
super.onActivityResult(请求代码、结果代码、数据)
if(Activity.RESULT_OK==resultCode&&requestCode==GALLERY)
{
如果(数据!=null)
{
val contentURI=data.data
尝试
{
val bitmap=MediaStore.Images.Media.getBitmap(this.contentResolver,contentURI)
val round=RoundedBitmapDrawableFactory.create(资源,位图)
round.isCircular=true
val path=saveImage(位图)
Toast.makeText(this@MainActivity,“已保存图像!”,Toast.LENGTH\u SHORT.show()
imageview!!.setImageDrawable(圆形)
}
捕获(e:IOException){
e、 printStackTrace()
Toast.makeText(this@MainActivity,“失败!”,Toast.LENGTH\u SHORT.show()
}
}
}
else if(Activity.RESULT_OK==resultCode&&requestCode==CAMERA)
{
val缩略图=数据!!.extras!!.get(“数据”)为位图
val round=RoundedBitmapDrawableFactory.create(参考资料,缩略图)
round.isCircular=true
imageview!!.setImageDrawable(圆形)
保存图像(缩略图)
Toast.makeText(this@MainActivity,“已保存图像!”,Toast.LENGTH\u SHORT.show()
}
}
趣味saveImage(myBitmap:Bitmap):字符串{
val bytes=ByteArrayOutputStream()
myBitmap.compress(Bitmap.CompressFormat.JPEG,100,字节)
val wallperDirectory=File(getExternalFilesDir(null).toString()+IMAGE\u目录)
deleteRecursive()目录
//如果需要,让对象构建目录结构。
Log.d(“fee”,目录.toString())
如果(!wallperDirectory.exists())
{
wallperDirectory.mkdirs()
}
尝试
{
Log.d(“heel”,目录.toString())
val f=文件(目录,((Calendar.getInstance)()
.getTimeInMillis()).toString()+“.jpg”))
f、 createNewFile()
val fo=文件输出流(f)
fo.write(bytes.toByteArray())
MediaScannerConnection.scanFile(此,
arrayOf(f.getPath()),
arrayOf(“图像/jpeg”),空值)
fo.close()
Log.d(“标记”,“保存的文件::-->”+f.getAbsolutePath())
返回f.getAbsolutePath()
}
捕获(e1:IOException){
e1.printStackTrace()
}
返回“”
}
伴星{
私有val IMAGE_目录=“/demonuts”
}
//轮廓图像
我算出了-我忘了在var位图之后放入val rotatedBitmap=bitmap.rotate(90F)
来旋转它我算出了-我忘了在var位图之后放入val rotatedBitmap=bitmap.rotate(90F)
来旋转它