Hi guys I am building an application in which I want the user to take an image from camera and set it as a profile pic. The image is taken fine and saved but is always in LANDSCAPE. I want it to be in the correct orientation.
Before this post is flagged duplicate I have to say that I already tried multiple answer I searched in similar questions. The only one succeeded is the following. But when it close the app and reopen it the orientation resets.
My code:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == REQ_CODE){
if(resultCode == RESULT_OK){
getContentResolver().notifyChange(uri, null);
ContentResolver cr = getContentResolver();
try {
bmp = MediaStore.Images.Media.getBitmap(cr, uri);
fixOrientation();
imagePhoto = (ImageView)findViewById(R.id.waiterPhoto);
imagePhoto.setImageBitmap(bmp);
Toast.makeText(UserData.this, "Image has been saved to " + file.getAbsolutePath() + " as " + file.getName(), Toast.LENGTH_LONG).show();
editor.putString(fileName, uri.toString());
editor.commit();
} catch (IOException e) {
throw new RuntimeException(e.getMessage());
}
}else if(resultCode == RESULT_CANCELED){
Toast.makeText(UserData.this, "Action Cancelled", Toast.LENGTH_SHORT).show();
}
}
}
My fixOrientationMethod()
:
public void fixOrientation() {
if (bmp.getWidth() > bmp.getHeight()) {
Matrix matrix = new Matrix();
matrix.postRotate(90);
bmp = Bitmap.createBitmap(bmp , 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);
}
}
My onCreate()
method:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_data);
sharedPreferences = getSharedPreferences(fileName, MODE_PRIVATE);
editor = sharedPreferences.edit();
imagePhoto = (ImageView)findViewById(R.id.waiterPhoto);
String commited = sharedPreferences.getString(fileName, null);
if(commited != null) {
imagePhoto.setImageURI(Uri.parse(commited));
}
imagePhoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//ContentValues values = new ContentValues();
//values.put(MediaStore.Images.Media.TITLE, fileName);
//uri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "waiterProfile.jpg");
uri = Uri.fromFile(file);
//uri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(intent, REQ_CODE);
}
});
}
Try this:
in Your onCreate()
imagePhoto.setRotation(90);
In your activityResult()
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQ_CODE) {
if (resultCode == RESULT_OK) {
taken = true;
getContentResolver().notifyChange(uri, null);
ContentResolver cr = getContentResolver();
try {
bmp = MediaStore.Images.Media.getBitmap(cr, uri);
imagePhoto.setImageBitmap(bmp);
imagePhoto.setRotation(90);
Toast.makeText(UserData.this, "Image has been saved to " + file.getAbsolutePath() + " as " + file.getName(), Toast.LENGTH_LONG).show();
editor.putString(fileName, uri.toString());
editor.commit();
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
} else if (resultCode == RESULT_CANCELED) {
taken = false;
Toast.makeText(UserData.this, "Action Cancelled", Toast.LENGTH_SHORT).show();
}
}
}
It will work but i dont know if it will for every device!!!