2016-09-14 2 views
-2

URL에서 이미지를 가져 오는 중 압축하여 외부 메모리 장치에 저장하고 싶습니다. 나는이 오류를Android에서 재생 된 비트 맵을 압축 할 수 없습니다.

java.lang.IllegalStateException를 얻을 : 가 android.graphics.Bitmap에서 android.graphics.Bitmap.checkRecycled (Bitmap.java:400) 에서 재활용 비트 맵 를 압축 할 수 없습니다. 압축 (Bitmap.java:1307)이 라인

mIcon11.compress(Bitmap.CompressFormat.JPEG, 40, bytes); 

에서

String foto = UT_drive_dropbox.AM.getfoto(); 
Bitmap mIcon11 = null; 
    try { 
     InputStream in = new java.net.URL(foto).openStream(); 
     mIcon11 = BitmapFactory.decodeStream(in); 
    } catch (Exception e) { 
     Log.e("Error", e.getMessage()); 
     e.printStackTrace(); 
    } 
final Bitmap output = Bitmap.createBitmap(mIcon11.getWidth(), 
      mIcon11.getHeight(), Bitmap.Config.ARGB_8888); 
    final Canvas canvas = new Canvas(output); 

    final int color = Color.RED; 
    final Paint paint = new Paint(); 
    final Rect rect = new Rect(0, 0, mIcon11.getWidth(), mIcon11.getHeight()); 
    final RectF rectF = new RectF(rect); 

    paint.setAntiAlias(true); 
    canvas.drawARGB(0, 0, 0, 0); 
    paint.setColor(color); 
    canvas.drawOval(rectF, paint); 

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); 
    canvas.drawBitmap(mIcon11, rect, rect, paint); 

    mIcon11.recycle(); 


    String fileName = "avatar.jpg"; 
    ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
    mIcon11.compress(Bitmap.CompressFormat.JPEG, 40, bytes); 

    File sd = new File(Environment.getExternalStorageDirectory(), getString(R.string.app_name) + File.separator + fileName); 

    FileOutputStream fileOutputStream = null; 
    try { 
     sd.createNewFile(); 
     fileOutputStream = new FileOutputStream(sd); 
     fileOutputStream.write(bytes.toByteArray()); 

    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } finally { 
     if (fileOutputStream != null) { 
      try { 
       fileOutputStream.close(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
    } 

답변

1

오류 메시지는 "재생 된 비트 맵을 압축 할 수 없습니다"입니다.

mIcon11.recycle(); 

그래서 하나가 recycle() 제거 또는 당신이 Bitmap 지적과 함께 할 의도 모든했을 후 로 이동 : 당신이 당신의 mIcon11.compress(Bitmap.CompressFormat.JPEG, 40, bytes); 행 앞에 세 줄을 보면, 당신은 볼 것이다 까지 mIcon11.

+2

그냥 이것을 덧붙이면,'recycle()'은 당신이 그 객체로 작업 할 때 호출되어야합니다. 그 물건을 가지고 전화를하는 것은 100 % 잘못되었습니다. 음료를 마시는 것, 음료수를 마시고 음료수를 마시는 것, 음료수를 마실 수없는 이유가 궁금한 것 같습니다. – CodyEngel