大家好我正在构建一个应用程序,我希望用户从相机中获取图像并将其设置为配置文件图片。图像拍摄得很好并保存,但总是在LANDSCAPE中。我希望它处于正确的方向。
在此帖被标记为重复之前,我必须说我已经尝试了多个答案,我在类似的问题中搜索过。唯一成功的是以下内容。但是当它关闭应用程序并重新打开它时,方向会重置。
我的代码:
@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);
}
});
}
答案 0 :(得分:0)
试试这个:
在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();
}
}
}
它会工作,但我不知道它是否适用于每个设备!!!