마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

bitmap outofmemory 질문 입니다.

0 추천

안드로이드 자바 쌩초보구요.

어찌어찌 앱을 하나 완성해서 잘 돌아가고있는상태입니다.

이번에 파일 업로드 기능 업데이트 때문에 또 여기저기 돌아다니면서 삽질을 좀 하고 있네요.

질문 내용은 제목그대로 outofmemory 관련입니다.

 

private class Photo_Select {
@SuppressWarnings("unused")
public void returnMessage2(final String article) {
callArticleStr = article;
new AlertDialog.Builder(MyArticleRegist.this)
.setTitle("사진 업로드")
.setItems(R.array.photoselect, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if(which==0){
Intent intent_gallery = new Intent();
intent_gallery.setAction(Intent.ACTION_GET_CONTENT);
intent_gallery.setType("image/*");
startActivityForResult(intent_gallery, REQ_IMAGE_SELECT);
}else{
Intent intent_camera = new Intent();
intent_camera.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent_camera, CODE_IMAGE_CAPTURE);
}
}
})
.setNegativeButton("취소", null)
.show();
}
}
 
이렇게 사진을 앨범과 내장 카메라를 이용한 사진찍기 두가지 방법으로 만들었구요.
 
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
No++; 
try {
if (requestCode == REQ_IMAGE_SELECT) {
 
Uri selPhoneUri = data.getData();
String[] proj = { MediaColumns.DATA };
Cursor cursor = getContentResolver().query(selPhoneUri, proj, null, null, null);
int index = cursor.getColumnIndex(MediaColumns.DATA);
cursor.moveToFirst();
String fullPath = cursor.getString(index);
 
int tempWidth = getBitmapOfWidth(fullPath.toString());
int tempHeight = getBitmapOfHeight(fullPath.toString());
 
if(tempWidth>600) tempWidth = 600;
if(tempHeight>800) tempHeight = 800;
 
Uri bitmapUri = Uri.fromFile(new File(fullPath));
 
Matrix matrix = new Matrix();
matrix.postRotate(90);
 
if(tempWidth>tempHeight){
Bitmap retateBitmap = Bitmap.createBitmap(BitmapFactory.decodeFile(bitmapUri.getPath()), 0, 0, tempWidth, tempHeight, matrix, true);
retateBitmap = Bitmap.createScaledBitmap(retateBitmap, 600, 800, true);
//Bitmap retateBitmap = Bitmap.
String fileName = System.currentTimeMillis()+".jpg";
saveImage(retateBitmap, fileName);
 
cursor.close();
DoFileUpload(urlString, "sdcard/DCIM/"+fileName);
 
String fileArray[] = fullPath.split("/", 0);
Log.i("fileName", fileName);
webView.loadUrl("javascript:loadImg1('"+fileName+"','"+No+"')");
}else{
Bitmap retateBitmap = BitmapFactory.decodeFile(bitmapUri.getPath());
String fileName = System.currentTimeMillis()+".jpg";
saveImage(retateBitmap, fileName);
 
cursor.close();
DoFileUpload(urlString, "sdcard/DCIM/"+fileName);
 
String fileArray[] = fullPath.split("/", 0);
Log.i("fileName", fileName);
webView.loadUrl("javascript:loadImg1('"+fileName+"','"+No+"')");
}
 
} else if (requestCode == CODE_IMAGE_CAPTURE) {
if (resultCode == RESULT_CANCELED) { return; }
 
Uri uriImages = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
String[] IMAGE_PROJECTION = { MediaColumns.DATA, BaseColumns._ID, };
 
try {
Cursor cursorImages = getContentResolver().query(uriImages, IMAGE_PROJECTION, null, null, null);
 
if (cursorImages != null && cursorImages.moveToLast()) {
fromFile = new File(cursorImages.getString(0));
 
int tempWidth = getBitmapOfWidth(fromFile.toString());
int tempHeight = getBitmapOfHeight(fromFile.toString());
 
String fullPath = fromFile.toString();
 
Uri bitmapUri = Uri.fromFile(new File(fullPath));
 
Matrix matrix = new Matrix();
matrix.postRotate(90);
 
if(tempWidth>tempHeight) {
Bitmap retateBitmap;
BitmapFactory.Options option = new BitmapFactory.Options();
option.inSampleSize = 1;
option.inPurgeable = true;
option.inDither = true;
 
retateBitmap = Bitmap.createBitmap(BitmapFactory.decodeFile(bitmapUri.getPath()), 0, 0, tempWidth, tempHeight, matrix, true);
retateBitmap = Bitmap.createScaledBitmap(retateBitmap, 600, 800, true);
String fileName = System.currentTimeMillis()+".jpg";
saveImage(retateBitmap, fileName);
 
DoFileUpload(urlString, "sdcard/DCIM/"+fileName);
 
String fileArray[] = fullPath.split("/", 0);
 
webView.loadUrl("javascript:loadImg1('"+fileName+"','"+No+"')");
}else {
Bitmap retateBitmap;
BitmapFactory.Options option = new BitmapFactory.Options();
option.inSampleSize = 1;
option.inPurgeable = true;
option.inDither = true;
 
retateBitmap = BitmapFactory.decodeFile(bitmapUri.getPath());
String fileName = System.currentTimeMillis()+".jpg";
saveImage(retateBitmap, fileName);
 
DoFileUpload(urlString, "sdcard/DCIM/"+fileName);
 
String fileArray[] = fullPath.split("/", 0);
 
webView.loadUrl("javascript:loadImg1('"+fileName+"','"+No+"')");
}
 
}
} catch (Exception e) {
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
 
사실 코드 이해도 좀 버겁네요.

암튼 위와 같이 코딩된 상태구요.

앨범에서 선택해서 올리는기능은 어쨌든 이상없이 잘 올라가고 보여지고 합니다.

문제는 카메라로 사진을 찍어서 업로드 하면 outofmemory 에러가 나네요.

어느 부분을 어떻게 손봐야할지 감조차 잡히지 않네요.

도움좀 부탁드리겠습니다.

 

별님반선생님 (200 포인트) 님이 2013년 8월 27일 질문
별님반선생님님이 2013년 8월 27일 수정

2개의 답변

0 추천
BitmapFactory.Options option = new BitmapFactory.Options();
option.inSampleSize = 1;
option.inPurgeable = true;
option.inDither = true;
 이부분에 
option.inSampleSize = 2; 로 변환 해서 해보세요 근데 품질은 떨어집니다. ;;;;; 
 
웅바라지 (4,780 포인트) 님이 2013년 8월 28일 답변
0 추천

1. 메니페스트 어플리케이션에 속성을준다. (허니콤이상부터)

        android:largeHeap="true"

2. 카메라로찍은 이미지를 리싸이징한다 (카메라로 찍은 이미지는 메모리가 커요)

 

이건 이미지 파싱할때 쓰던 메소드인데 찾아보시면있을듯

private Bitmap dt,resized;
private BufferedInputStream bis;
 
Bitmap imgparser(String imgurl){
 
try {
 
URL url = new URL(imgurl);
 
URLConnection conn = url.openConnection();
 
 
 
bis = new BufferedInputStream(
conn.getInputStream()); 
 
dt = BitmapFactory.decodeStream(bis);
 
int height = dt.getHeight();
int width = dt.getWidth();
resized = null;
while (height > 128) {
resized = Bitmap.createScaledBitmap(dt, (width * 128) / height, 128, true);
height = resized.getHeight();
width = resized.getWidth();
}
 
 
bis.close();
 
} catch (Exception e) {
resized=null;
}finally{
 
}
 
return resized;
}

 

열심히잘할게요 (1,260 포인트) 님이 2013년 8월 29일 답변
...