這是我的第一篇文章,雖然我是Android新手,但這個社區迄今爲止一直很棒。當我在相機應用程序中旋轉屏幕並且我的主要活動失敗時,Android相機沒有將照片返回到主要活動
這是我用我的相對簡單的應用程序遇到的麻煩。
我對我的主要活動有圖像視圖。 onClick,圖像視圖將打開相機應用程序。我可以拍攝照片,相機應用程序將返回該照片並將其設置爲圖像視圖中的照片。
當我從一個方向開始並在另一個方向拍照時,主要活動將崩潰。示例:我首先以垂直方向打開主要活動,打開相機應用程序並切換到水平視圖,然後拍攝照片。當相機應用程序嘗試返回圖片時,主要活動崩潰。當所有活動以相同方向使用時,應用程序不會崩潰。
我開始相信這是我保存圖片的方式,或者onResume,主要活動沒有足夠的時間切換到新的方向並在圖像崩潰之前接收圖片。或者當主要活動恢復時它可能會破壞圖片。
public class mainActivity extends Activity {
static final int REQUEST_IMAGE_CAPTURE = 1;
//Declares variable of mImageView
ImageView mImageView;
String mCurrentPhotoPath;
//String activityOrientation;
File photoFile = null;
Uri uriFile;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Setting mImageView to the UI ID of
mImageView = (ImageView) findViewById(R.id.imageViewLicense); // making mImageView = the license image view right now just to make sure the code works, I can add to later when adding the medical card
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
if (savedInstanceState.containsKey("cameraImageUri")) {
uriFile = Uri.parse(savedInstanceState.getString("cameraImageUri"));
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if (uriFile != null) {
outState.putString("cameraImageUri", uriFile.toString());
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
//Initialized the camera to take a picture then return the result of the picture
public void dispatchTakePictureIntent(View view)
{
//Create new intent
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
try {
photoFile = createImageFile();
} catch (IOException ex) {
}
// Continue only if the File was successfully created
if (photoFile != null) {
uriFile = Uri.fromFile(photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
uriFile);
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
}
//Method for rotating the image of the bitmap to the corrected orientation
private Bitmap adjustImageOrientation(Bitmap image) {
ExifInterface exif;
try {
exif = new ExifInterface(mCurrentPhotoPath);
int exifOrientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
int rotate = 0;
switch (exifOrientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
rotate = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotate = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
rotate = 270;
break;
}
if (rotate != 0) {
int w = image.getWidth();
int h = image.getHeight();
// Setting pre rotate
Matrix mtx = new Matrix();
mtx.preRotate(rotate);
// Rotating Bitmap & convert to ARGB_8888, required by tess
image = Bitmap.createBitmap(image, 0, 0, w, h, mtx, false);
}
} catch (IOException e) {
return null;
}
return image.copy(Bitmap.Config.ARGB_8888, true);
}
//The photo taken in the takePicture method is returned here
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Get the dimensions of the View
int targetW = mImageView.getWidth();
int targetH = mImageView.getHeight();
// Get the dimensions of the bitmap
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
int photoW = bmOptions.outWidth;
int photoH = bmOptions.outHeight;
// Determine how much to scale down the image
int scaleFactor = Math.min(photoW/targetW, photoH/targetH);
// Decode the image file into a Bitmap sized to fill the View
bmOptions.inJustDecodeBounds = false;
bmOptions.inSampleSize = scaleFactor;
bmOptions.inPurgeable = true;
Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
//Rotates the image to the proper orientation
Bitmap adjustedBitmap = adjustImageOrientation(bitmap);
//Sets the original imageview as picture taken.
mImageView.setImageBitmap(adjustedBitmap);
}
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = image.getAbsolutePath();
return image;
}
}
發佈崩潰日誌 – Mus 2014-12-04 03:06:00