ImageView旋转后上传服务器仍显示原图问题求助
Hey there! I’ve dealt with this exact problem before, so let me walk you through why this is happening and how to fix it.
The imageView.setRotation(imageView.getRotation() + 90) call only changes how the ImageView displays the image on your screen—it doesn’t modify the actual image file or the pixel data of the bitmap behind the view. When you upload, you’re still sending the original unrotated file/bitmap, not the rotated version you see on screen.
You need to create a new bitmap that actually reflects the rotation, then convert that bitmap into a file you can upload via Retrofit. Here’s how to do it:
1. Create a Rotated Bitmap
First, make a helper method to generate a rotated version of your original bitmap:
// Java example public Bitmap getRotatedBitmap(Bitmap originalBitmap, float rotationDegrees) { Matrix matrix = new Matrix(); matrix.postRotate(rotationDegrees); return Bitmap.createBitmap(originalBitmap, 0, 0, originalBitmap.getWidth(), originalBitmap.getHeight(), matrix, true); }
// Kotlin example fun getRotatedBitmap(originalBitmap: Bitmap, rotationDegrees: Float): Bitmap { val matrix = Matrix() matrix.postRotate(rotationDegrees) return Bitmap.createBitmap(originalBitmap, 0, 0, originalBitmap.width, originalBitmap.height, matrix, true) }
2. Convert the Rotated Bitmap to a File
Next, save this rotated bitmap to a temporary file (so Retrofit can read it for upload):
// Java example public File bitmapToFile(Context context, Bitmap bitmap, String fileName) throws IOException { File file = new File(context.getCacheDir(), fileName + ".jpg"); OutputStream outStream = new FileOutputStream(file); bitmap.compress(Bitmap.CompressFormat.JPEG, 90, outStream); outStream.flush(); outStream.close(); return file; }
// Kotlin example fun bitmapToFile(context: Context, bitmap: Bitmap, fileName: String): File { val file = File(context.cacheDir, "$fileName.jpg") file.outputStream().use { outStream -> bitmap.compress(Bitmap.CompressFormat.JPEG, 90, outStream) outStream.flush() } return file }
Tweak the compression format (PNG/JPEG) and quality (the 90 value) based on your needs.
3. Upload the Rotated File
Now, instead of using your original image file, use this new rotated file for your Retrofit multipart request:
// Java example Bitmap rotatedBitmap = getRotatedBitmap(originalBitmap, imageView.getRotation()); File rotatedFile = bitmapToFile(context, rotatedBitmap, "rotated_image"); RequestBody requestFile = RequestBody.create(MediaType.parse("image/jpeg"), rotatedFile); MultipartBody.Part imagePart = MultipartBody.Part.createFormData("image", rotatedFile.getName(), requestFile); // Call your Retrofit upload service yourApiService.uploadImage(imagePart).enqueue(new Callback<YourResponse>() { // Handle callback logic here });
// Kotlin example val rotatedBitmap = getRotatedBitmap(originalBitmap, imageView.rotation) val rotatedFile = bitmapToFile(context, rotatedBitmap, "rotated_image") val requestFile = RequestBody.create(MediaType.parse("image/jpeg"), rotatedFile) val imagePart = MultipartBody.Part.createFormData("image", rotatedFile.name, requestFile) // Call your Retrofit upload service yourApiService.uploadImage(imagePart).enqueue(object : Callback<YourResponse> { // Handle callback logic here })
Pro Tip: Clean Up Temporary Files
After the upload completes (success or failure), delete the temporary file to free up storage:
// Java if (rotatedFile.exists()) { rotatedFile.delete(); }
// Kotlin if (rotatedFile.exists()) { rotatedFile.delete() }
- Make sure you track the total rotation applied (each click adds 90 degrees) so you pass the correct value to
getRotatedBitmap(). - If you’re working with large images, add resizing logic to avoid OutOfMemoryErrors—you can resize the bitmap before rotating it if needed.
内容的提问来源于stack exchange,提问作者DevMobApp

