특정폰에서(구형폰)에서 카메라에서 찍은 사진을 저장할때 에러가 나네요
Throwing OutOfMemoryError "Failed to allocate a 51916812 byte allocation with 12763771 free bytes and 12MB until OOM"
이미지 파일이 커서 그런거 같은데 largeHeap="true" 으로 일단 해결은 했는데
제대로 된 해결책은 아닌거 같아서요
그리고 제 앱에서 저장하면 이미지 사진이 6M인데 폰의 기본 앱으로 찍으면 2M정도가 나오네요
참고로 해상도는 최고 해상도로 찍을려고 하고 제 앱과 폰의 기본 카메라 앱도 해상도는 같습니다
혹시 방법 있을까요?
Bitmap.createBitmap 에서 에러가 나고 이 부분을 빼버리면(회전을 안하고 저장을 하면)
에러가 없습니다 하지만 저는 저장할때 회전을 하고 싶은데 가능할까요?
소스는 아래와 같습니다 감사합니다^^
private class SaveImageTask extends AsyncTask<byte[], Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(byte[]... data) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeByteArray(data[0], 0, data[0].length, options);
Log.i("sinwhod","matrix = " + orientation);
Matrix matrix = new Matrix();
//matrix.postRotate(orientation);
matrix.setRotate(orientation);
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] currentData = stream.toByteArray();
Date day = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault());
fileName = String.valueOf(sdf.format(day));
savePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString();
temp = savePath + "/" + fileName + ".jpg";
File file = new File(temp);
try {
FileOutputStream fos = new FileOutputStream(file);
fos.write(currentData);
fos.flush();
fos.close();
} catch (Exception e) {
Log.i("sinwhod", "error = " + e);
}
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + temp)));
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
Toast.makeText(MainActivity.this, temp, Toast.LENGTH_SHORT).show();
}
}