从相机拍摄图像时 Intent 始终是风景
When Image is taken from camera Intent is always landscape
大家好我正在构建一个应用程序,我希望用户在其中从相机拍摄图像并将其设置为个人资料照片。图像拍摄和保存都很好,但始终是风景。我希望它处于正确的方向。
在此 post 被标记为重复之前,我不得不说我已经尝试过在类似问题中搜索过的多个答案。唯一成功的是以下。但是当它关闭应用程序并重新打开它时,方向会重置。
我的代码:
@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();
}
}
}
我的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);
}
}
我的onCreate()
方法:
@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);
}
});
}
试试这个:
在你的 onCreate()
imagePhoto.setRotation(90);
在你的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();
}
}
}
它会起作用,但我不知道它是否适用于所有设备!!!
大家好我正在构建一个应用程序,我希望用户在其中从相机拍摄图像并将其设置为个人资料照片。图像拍摄和保存都很好,但始终是风景。我希望它处于正确的方向。
在此 post 被标记为重复之前,我不得不说我已经尝试过在类似问题中搜索过的多个答案。唯一成功的是以下。但是当它关闭应用程序并重新打开它时,方向会重置。
我的代码:
@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();
}
}
}
我的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);
}
}
我的onCreate()
方法:
@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);
}
});
}
试试这个:
在你的 onCreate()
imagePhoto.setRotation(90);
在你的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();
}
}
}
它会起作用,但我不知道它是否适用于所有设备!!!