Reputation: 694
I would like to rotate and save the rotated image and move it elsewhere within my android device.
The only thing I am unable to do is get the saved rotated image FILE (rotated.jpg)
My code below to rotate: (this does not save rotated file to storage?)
Bitmap bmp = BitmapFactory.decodeFile(filePathLocal);
Matrix matrix = new Matrix();
matrix.postRotate(getImageOrientation(filePathLocal));
rotatedBitmap = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);
//Set Image
ImageButton ibProfile = (ImageButton) findViewById(R.id.ibProfile);
ibProfile.setImageBitmap(rotatedBitmap);
Now the above only rotates temporarily until the activity is ended, now I would like to save this rotated image from above code and move it somehwere before uploading it to server, I already know how to copy/move files and upload so no need to post those codes - all I need is the code for SAVING ROTATED IMAGE, so that I have something like /sdcard/saved_rotated_image.jpg
Upvotes: 1
Views: 2701
Reputation: 109
Save the image function is as follows:
private void saveBitmap(Bitmap bitmap, String fileName) {
File file = new File(Environment.getExternalStorageDirectory(), fileName);
FileOutputStream fileOS = null;
try {
fileOS = new FileOutputStream(file);
// quality: Hint to the compressor, 0-100. 0 meaning compress for small size, 100 meaning compress for max quality.
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOS);
fileOS.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileOS != null) {
try {
fileOS.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Use the sample:
Bitmap bmp = BitmapFactory.decodeFile(filePathLocal);
Matrix matrix = new Matrix();
matrix.postRotate(getImageOrientation(filePathLocal));
rotatedBitmap = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);
saveBitmap(rotatedBitmap, "saved_rotated_image.jpg");
Upvotes: 1
Reputation: 597
Bitmap bmp = BitmapFactory.decodeFile(filePathLocal);
Matrix matrix = new Matrix();
matrix.postRotate(getImageOrientation(filePathLocal));
rotatedBitmap = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, false);
//Set Image
ImageButton ibProfile = (ImageButton) findViewById(R.id.ibProfile);
ibProfile.setImageBitmap(rotatedBitmap);
Put false in the createbitmap's last argument
Refere my code:
@Override
protected Uri doInBackground(String... params) {
String filepath = params[0];
String filename = params[1];
String filetype = params[2];
Bitmap bitmap = takeScreenShot(root);
Matrix rotateMatrix = new Matrix();
rotateMatrix.postRotate(Datas.rotationvalue);
Log.e("rotationvalue", Datas.rotationvalue+"...");
Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), rotateMatrix, false);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
rotatedBitmap.compress(Bitmap.CompressFormat.PNG, 100, bytes);
try {
File f = new File(filepath);
if (!f.exists()) {
f.mkdir();
}
String folderpath = f.toString();
File file = new File(folderpath, filename + "." + filetype);
file.createNewFile();
FileOutputStream fo = new FileOutputStream(file);
fo.write(bytes.toByteArray());
fo.close();
Uri uri = Uri.fromFile(file);
Log.e("Edited img uri", uri.toString());
return uri;
} catch (Exception e) {
Log.e("Exception...occured", e.toString());
e.printStackTrace();
return null;
}
}
This code is working fine.At my own side, try at your side.
According to doc: last argument is filter boolean: true if the source should be filtered. Only applies if the matrix contains more than just translation.
Link for more info: Last argument more info
Upvotes: 2