【发布时间】:2020-11-10 14:18:57
【问题描述】:
我需要上传一张图片到服务器。为此,我捕获图像(或从图库中获取图像)该图像被放置在其确切位置,但是当我将图像上传到服务器时,图像被旋转。
我也用谷歌搜索过,但没有运气。
以下是我的代码:
private fun chooseImage() {
val items = arrayOf<CharSequence>("Camera", "Gallery", "Cancel")
val builder = AlertDialog.Builder(this)
builder.setTitle("Add Attachment")
builder.setItems(items) { dialog, item ->
try {
if (items[item] == "Camera") {
takePhotoFromCamera()
} else if (items[item] == "Gallery") {
choosePhotoFromGallary()
} else if (items[item] == "Cancel") {
dialog.dismiss()
}
} catch (e: Exception) {
e.printStackTrace()
}
}
builder.show()
}
fun choosePhotoFromGallary() {
val galleryIntent = Intent(
Intent.ACTION_PICK,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI
)
startActivityForResult(galleryIntent, GALLERY)
}
private fun takePhotoFromCamera() {
val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
startActivityForResult(intent, CAMERA)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == GALLERY) {
if (data != null) {
val contentURI: Uri = data.getData()
try {
var bitmap =
MediaStore.Images.Media.getBitmap(this.contentResolver, contentURI)
imagePath = bitmapToFile(bitmap,this).toString()
var bitmapNew = handleSamplingAndRotationBitmap(this,contentURI)
binding.circleImageView.setImageBitmap(bitmapNew)
AppInstance.profileimagePath = imagePath
} catch (e: IOException) {
e.printStackTrace()
showToast(this, "Failed!")
}
}
} else if (requestCode == CAMERA) {
val thumbnail = data!!.getExtras().get("data") as Bitmap
imagePath= bitmapToFile(thumbnail,this).toString()
Glide.with(this)
.load(imagePath).into(binding.circleImageView);
AppInstance.profileimagePath = imagePath
}
}
这是:
@Throws(IOException::class)
public fun handleSamplingAndRotationBitmap(context: Context, selectedImage: Uri?): Bitmap? {
val MAX_HEIGHT = 800
val MAX_WIDTH = 800
// First decode with inJustDecodeBounds=true to check dimensions
val options = BitmapFactory.Options()
options.inJustDecodeBounds = true
var imageStream: InputStream = context.getContentResolver().openInputStream(selectedImage)
BitmapFactory.decodeStream(imageStream, null, options)
imageStream.close()
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, MAX_WIDTH, MAX_HEIGHT)
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false
imageStream = context.getContentResolver().openInputStream(selectedImage)
var img = BitmapFactory.decodeStream(imageStream, null, options)
img = selectedImage?.let { rotateImageIfRequired(context, img, it) }
return img
}
位图到文件:
fun bitmapToFile(bitmap: Bitmap, context: Context): Uri {
// Get the context wrapper
val dateFormat = SimpleDateFormat("yyyyMMdd_HH_mm_ss")
val currentTimeStamp = dateFormat.format(Date())
val wrapper = ContextWrapper(context)
// Initialize a new file instance to save bitmap object
var file = wrapper.getDir("Images",Context.MODE_PRIVATE)
file = File(file,"$currentTimeStamp.jpg")
var bitmapNew = bitmap
try{
val imageRotation: Int = getImageRotation(file)
if (imageRotation != 0)
bitmapNew = getBitmapRotatedByDegree(bitmap, imageRotation)!!
// Compress the bitmap and save in jpg format
val stream: OutputStream = FileOutputStream(file)
bitmapNew.compress(Bitmap.CompressFormat.JPEG,70,stream)
stream.flush()
stream.close()
}catch (e: IOException){
e.printStackTrace()
}
// Return the saved bitmap uri
return Uri.parse(file.absolutePath)
}
我们将非常感谢您的支持。 提前致谢。
【问题讨论】:
-
val imageRotation: Int = getImageRotation(file)您不应该从 File 对象而是从原始 data.getData() uri 中获取旋转。 10 岁以下不要使用 File 类。 -
现在解决了吗?
-
@AnshulTyagi 是的,实际上问题出在 bitmaptoFile 函数上。我改变了它并且它有效:)
标签: android android-studio kotlin file-upload bitmapimage