I am creating an application in which on click on the action bar item, the camera activity starts, and after capturing and saving the pic, it displays it on the main activity.
Now if i start the Camera from the MainActivity while in portrait orientation, and switch to landscape orientation in the Camera Activity, take the picture, and click on the Save button, it returns to the MainActivity, but image is not displayed, this is I guess because the main activity is getting reloaded, but I a not sure though.
How do I have to retain the image even after the change in orientation.
MainActivity.java
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_camera:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// file creation for saving video
Uri fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
if (fileUri != null) {
targetFile = fileUri.getPath();
// setting file image name
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
return true;
onActivityResult()
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// Image captured and saved to fileUri specified in the Intent
ImageView imgPicture = (ImageView) findViewById(R.id.imgPicture);
if (targetFile != null) {
theFile = targetFile;
imgPicture.setImageBitmap(BitmapFactory.decodeFile(theFile));
}
} else if (resultCode == RESULT_CANCELED) {
} else {
Toast.makeText(this, "Your picture could not be saved.", Toast.LENGTH_LONG).show();
}
}
}
Manifest.xml
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="17" />
<uses-feature
android:name="android.hardware.camera"
android:required="true" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.ActionBarTest.MainActivity"
android:label="@string/app_name"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
maybe you can use this to be able to know how the image saved and then make bitmap or whatever based on that
public static int getExifRotation(String imgPath)
{
try
{
ExifInterface exif = new ExifInterface(imgPath);
String rotationAmount = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
if (!TextUtils.isEmpty(rotationAmount))
{
int rotationParam = Integer.parseInt(rotationAmount);
switch (rotationParam)
{
case ExifInterface.ORIENTATION_NORMAL:
return 0;
case ExifInterface.ORIENTATION_ROTATE_90:
return 90;
case ExifInterface.ORIENTATION_ROTATE_180:
return 180;
case ExifInterface.ORIENTATION_ROTATE_270:
return 270;
default:
return 0;
}
}
else
{
return 0;
}
}
catch (Exception ex)
{
return 0;
}
}
also check
if(bitmap.getWidth() > bitmap.getHeight()){}
that will let you know if the picture is landscape because width would be greater than height and use the bitmap constructor that takes matrix to rotate:
Matrix matrix = new Matrix();
matrix.postRotate(90); //or whatever
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.