我正在創建一個Android應用程序,利用相機拍照,然後根據用戶輸入將它們發送到服務器。我目前在相機意圖上遇到了一些問題。我的主要問題是:Android相機意圖 - 內存不足錯誤和旋轉錯誤
獲取與拍攝位置相比似乎旋轉的圖片。
當我嘗試解決這個旋轉我得到一個OutOfMemoryError
所以主要我需要確保的是,照片的方向不會改變,我沒有得到一個OutOfMemoryError。
以下是使用相機Intent拍攝照片的功能。
private void dispatchTakePictureIntent(int actionCode) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File f = new File(Environment.getExternalStorageDirectory(),"/ServerApp/test.jpg");
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
startActivityForResult(takePictureIntent, actionCode);
}
此功能用於旋轉圖像並將其保存在該旋轉中。
private void getRotate() {
String imagePath ="";
int rotate = 0;
File f = null;
try {
f = new File(Environment.getExternalStorageDirectory(),"/ServerApp/test.jpg");
imagePath = f.getAbsolutePath();
File imageFile = new File(imagePath);
ExifInterface exif = new ExifInterface(
imageFile.getAbsolutePath());
int orientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_270:
rotate = 270;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotate = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_90:
rotate = 90;
break;
}
} catch (Exception e) {
e.printStackTrace();
}
Bitmap p = BitmapFactory.decodeFile(imagePath);
Matrix m = new Matrix();
m.setRotate(rotate);
Bitmap _bitmapScaled = Bitmap.createBitmap(p, 0, 0, p.getWidth()/2,p.getHeight(), m, false);
f.delete();
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
_bitmapScaled.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
//you can create a new file name "test.jpg" in sdcard folder.
File f1 = new File(Environment.getExternalStorageDirectory()
+ File.separator + "test.jpg");
try {
f1.createNewFile();
FileOutputStream fo = new FileOutputStream(f1);
fo.write(bytes.toByteArray());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//write the bytes in file
}
最後,這是我的onActivityResult方法:
protected void onActivityResult(int requestCode, int resultCode, Intent data){
getRotate();
File f = new File(Environment.getExternalStorageDirectory(),"/ServerApp/test.jpg");
mImageView.setImageBitmap(BitmapFactory.decodeFile(f.getAbsolutePath()));
}
我一直在試圖解決這個問題,很長一段時間,我會很感激,如果我能得到一些幫助。謝謝!