2013-11-25 1 views
1

웹 뷰에 html 데이터를로드하는 중입니다. 그런 다음 webview 데이터를 캡처하여 해당 이미지를 SD 카드에 저장해야합니다. 작은 이미지에 대해서는 정상적으로 작동하지만 큰 이미지에 대해서는 메모리 예외가 발생합니다. 나는 이것을하기 위해 다음의 논리를 사용하고있다.웹 뷰 캡처로 인해 메모리가 부족합니다.

private void generateImg() { 
    // TODO Auto-generated method stub 
     try{ 

      Picture p = webview.capturePicture(); 
      Bitmap bitmap=pictureDrawable2Bitmap(p); 
      String root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).toString(); 
      File myDir = new File(root + "/Aack"); 
      if(myDir.exists()) 
      { 
       //Log.e("Directory","Existed"); 
      } 
      else 
      { 
       myDir.mkdir(); 
      } 
      String fname = System.currentTimeMillis()+".png"; 
      //Log.e("file name...",""+fname); 
      file = new File (myDir, fname); 
      try 
      { 
       FileOutputStream out = new FileOutputStream(file); 
       bitmap.compress(Bitmap.CompressFormat.PNG, 100, out); 
       out.flush(); 
       out.close(); 

      } 
      catch (Exception e) 
      { 
        e.printStackTrace(); 
      } 
      file_name=myDir+"/"+fname; 


     }catch(Exception e) 
     { 
     e.printStackTrace(); 
     } 

} 




private static Bitmap pictureDrawable2Bitmap(Picture picture){ 
     PictureDrawable pictureDrawable = new PictureDrawable(picture); 
     Bitmap bitmap = Bitmap.createBitmap(pictureDrawable.getIntrinsicWidth(),pictureDrawable.getIntrinsicHeight(), Config.ARGB_8888); 
     Canvas canvas = new Canvas(bitmap); 
     canvas.drawPicture(pictureDrawable.getPicture()); 
     return bitmap; 
    } 

그래서 어떻게 처리해야합니까? 감사합니다

+1

작고 큰 이미지가 무엇을 의미하는지 알고 싶습니다. 이미지가 webView로로드되거나 이미지를 캡처합니까? – vinaykumar

+1

어느 라인에서 예외를 던지고 있습니까? –

+0

비트 맵 bitmap = Bitmap.createBitmap (pictureDrawable.getIntrinsicWidth(), pictureDrawable.getIntrinsicHeight(), Config.ARGB_8888); @Bharat Sharma – koti

답변

0

@varan, 이미지 디코딩하려고 말했듯이 : 나는 다른 사람이 부담없이 알고, 그래서 만약 사진에서 비트 맵에 특별한 경우에 그것을 할 방법을 모르는

public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, 
     int reqWidth, int reqHeight) { 

     // First decode with inJustDecodeBounds=true to check dimensions 
     final BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inJustDecodeBounds = true; 
     BitmapFactory.decodeResource(res, resId, options); 

     // Calculate inSampleSize 
     options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); 

     // Decode bitmap with inSampleSize set 
     options.inJustDecodeBounds = false; 
     return BitmapFactory.decodeResource(res, resId, options); 
} 

    public static int calculateInSampleSize(
     BitmapFactory.Options options, int reqWidth, int reqHeight) { 
     // Raw height and width of image 
     final int height = options.outHeight; 
     final int width = options.outWidth; 

     int stretch_width = Math.round((float)width/(float)reqWidth); 
     int stretch_height = Math.round((float)height/(float)reqHeight); 

     if (stretch_width <= stretch_height) return stretch_height; 
     else return stretch_width; 
} 

을 이 게시물을 편집하십시오.

관련 문제