2013-04-27 4 views
1
Intent localIntent = new Intent(
       android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
     File dir = Environment 
       .getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM); 
     random = new Random(); 
     seqNo = random.nextInt(1000000); 
     photoName = String.valueOf(seqNo); 
     output = new File(dir, photoName + ".jpeg"); 
     imgPath = Uri.fromFile(output); 
     localIntent.putExtra(MediaStore.EXTRA_OUTPUT, imgPath); 
     path = output.getAbsolutePath(); 
     startActivityForResult(localIntent, PICK_Camera_IMAGE); 


protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
      if (requestCode== PICK_Camera_IMAGE && resultCode == RESULT_OK) { 


         try { 


          File f = new File(path); 

          Matrix mat = new Matrix(); 
          mat.postRotate(90); 
// 
          Bitmap bmp = BitmapFactory.decodeStream(new FileInputStream(f), null, null); 
          Bitmap bmpPic = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), mat, true); 

참고 : i 카메라로 사진을 찍을 때이 코드를 사용합니다. 그것은 삼성 갤럭시 s3을 제외한 모든 장치에서 실행됩니다. 내가 s3에서 실행하면 OutOfMemoryError와 같은 오류가 발생합니다.은하계의 OutOfMemoryError

로그 캣 :

04-27 11:26:49.775: E/AndroidRuntime(6356): java.lang.OutOfMemoryError 
04-27 11:26:49.775: E/AndroidRuntime(6356):  at android.graphics.Bitmap.nativeCreate(Native Method) 
04-27 11:26:49.775: E/AndroidRuntime(6356):  at android.graphics.Bitmap.createBitmap(Bitmap.java:640) 
04-27 11:26:49.775: E/AndroidRuntime(6356):  at android.graphics.Bitmap.createBitmap(Bitmap.java:586) 
04-27 11:26:49.775: E/AndroidRuntime(6356):  at com.example.camera.CameraAct$10$1.run(CameraAct.java:378) 
04-27 11:26:49.775: E/AndroidRuntime(6356):  at android.os.Handler.handleCallback(Handler.java:615) 
04-27 11:26:49.775: E/AndroidRuntime(6356):  at android.os.Handler.dispatchMessage(Handler.java:92) 
04-27 11:26:49.775: E/AndroidRuntime(6356):  at android.os.Looper.loop(Looper.java:137) 
04-27 11:26:49.775: E/AndroidRuntime(6356):  at android.app.ActivityThread.main(ActivityThread.java:4898) 
04-27 11:26:49.775: E/AndroidRuntime(6356):  at java.lang.reflect.Method.invokeNative(Native Method) 
04-27 11:26:49.775: E/AndroidRuntime(6356):  at java.lang.reflect.Method.invoke(Method.java:511) 
04-27 11:26:49.775: E/AndroidRuntime(6356):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006) 

답변

2

사용하기 전에 비트 맵의 ​​샘플 크기를 줄여야합니다. 이를 사용하여이를 수행 할 수 있습니다.

private Bitmap decodeFile(File file) 
{ 
    try 
    { 
     //********************* decode image size ******************** 
     BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inJustDecodeBounds = true; 
     BitmapFactory.decodeStream(new FileInputStream(file), null, options); 

     // ********************** Find the correct scale value. It should be the power of 2. ******************** 
     options.inSampleSize = BitmapConverter.calculateInSampleSize(options, 145, 105); 
     options.inJustDecodeBounds = false; 

     return BitmapFactory.decodeStream(new FileInputStream(file), null, options); 
    } 
    catch(FileNotFoundException eFileNotFoundException) 
    { 

     return null; 
    } 
} 

그리고 필요하지 않은 경우 Bitmap.recycle()을 호출 할 수 있습니다.

0
BitmapFactory.Options options = new BitmapFactory.Options(); 
// will results in a much smaller image than the original 
options.inSampleSize = 8; 
Bitmap bitmap = BitmapFactory.decodeFile(PATH,options); 

는 또한 도움이 될 수 this 대답을 살펴.

+0

PATH 란 무엇입니까 ?? – Riser

+0

파일 f = 새 파일 (** 경로 **); – MAC