私はしばらくの間、この問題と戦ってきました。私のカメラ アクティビティはポートレート モードにロックされており、他の機能が原因で、ポートレート モードである必要があります。カメラで写真を撮って表示すると、回転して表示されます。この問題を処理する解決策を見つけましたが、弱いデバイスのメモリが多すぎるため、[写真を撮る] ボタンをクリックした後、次のアクティビティ (プレビュー アクティビティ) に進むまで約 8 秒待たなければなりませんが、自分の Nexus 4 では動作しません。遅れてすぐに起動します。私の現在のコードは次のようになります。
private Camera.PictureCallback mPicture = new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
File pictureFile = getOutputMediaFile();
if (pictureFile == null){
return;
}
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
} catch (FileNotFoundException e) {
Log.d("ABC", "File not found: " + e.getMessage());
} catch (IOException e) {
Log.d("ABC", "Error accessing file: " + e.getMessage());
}
Bitmap bitmap = BitmapFactory.decodeFile(pictureFile.getAbsolutePath());
Matrix matrix = new Matrix();
if (currentCamera == BACK_CAMERA) {
matrix.postRotate(90);
} else {
matrix.postRotate(270);
matrix.preScale(1.0f, -1.0f);
}
Bitmap result = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
try {
FileOutputStream outStream = new FileOutputStream(pictureFile);
result.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
outStream.flush();
outStream.close();
} catch (FileNotFoundException e) {
Log.d("ABC", "File not found: " + e.getMessage());
} catch (IOException e) {
Log.d("ABC", "Error accessing file: " + e.getMessage());
}
takenPhotoURL= pictureFile.getAbsolutePath();
Intent intent = new Intent(CameraActivity.this, PreviewActivity.class);
intent.putExtra("PhotoURI",takenPhotoURL);
intent.putExtra("voteID",voteId);
CameraActivity.this.startActivity(intent);
}
getOutputMediaFile は、デバイスに写真を保存するディレクトリを生成するメソッドです。(まだギャラリーに写真を追加していません)。上記のコードを分析すると、FileOutputStream を 2 回使用していることがわかります。初めて写真を電話のメモリに保存し、2回目にもう一度開いて、回転したマトリックスに書き込んでフラッシュします。FileOutputStream を使用すると、保存 -> 開く -> 保存 -> 閉じる -> 開く -> 保存 - 閉じると、弱いデバイスが遅くなると思います。このメソッドを何らかの方法で変更して、より効率的にしたいと考えています。それを手伝ってください。
私が今まで試したのは、最初の FileOutputStream を削除し、すべてを一度にそのようにしようとすることです:
private Camera.PictureCallback mPicture = new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
File pictureFile = getOutputMediaFile();
if (pictureFile == null){
return;
}
try {
FileOutputStream outStream = new FileOutputStream(pictureFile);
outStream.write(data);
Bitmap bitmap = BitmapFactory.decodeFile(pictureFile.getAbsolutePath());
Matrix matrix = new Matrix();
if (currentCamera == BACK_CAMERA) {
matrix.postRotate(90);
} else {
matrix.postRotate(270);
matrix.preScale(1.0f, -1.0f);
}
Bitmap result = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
result.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
outStream.flush();
outStream.close();
} catch (FileNotFoundException e) {
Log.d("ABC", "File not found: " + e.getMessage());
} catch (IOException e) {
Log.d("ABC", "Error accessing file: " + e.getMessage());
}
takenPhotoURL= pictureFile.getAbsolutePath();
Intent intent = new Intent(CameraActivity.this, PreviewActivity.class);
intent.putExtra("PhotoURI",takenPhotoURL);
intent.putExtra("voteID",voteId);
CameraActivity.this.startActivity(intent);
}
しかし、うまくいきません。写真はそのまま表示されます (何も起こりません)。また、Exifも試しましたが、効果がありませんでした。より効率的かつ高速にするのを手伝ってください。