0

J'essaye de dessiner une image bitmap sur le canevas. Mais ce faisant, je reçois l'exception spécifiée ci-dessous.Fatal signal 11 (SIGSEGV), code 1, erreur addr 0xc0 dans tid 20706 en essayant de dessiner bitmap sur le canevas

10-10 11:29:18.592: A/libc(20706): Fatal signal 11 (SIGSEGV), code 1, fault addr 0xc0 in tid 20706 (apcanvasexample) 

Voici mon code.

public class MapView extends View implements IAsyncResponse { 
    private Path path; 
    private Paint paint; 

    private URL url; 

    private Bitmap bmp; 
    private Canvas canvas; 

    public MapView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     path = new Path(); 
     paint = new Paint(); 
     paint.setStyle(Paint.Style.STROKE); 
     paint.setColor(Color.RED); 
     paint.setStrokeWidth(10); 
     path.moveTo(0, 0); 

    } 

    @SuppressLint("DrawAllocation") 
    @Override 
    public void onDraw(Canvas canvas) { 
     this.canvas = canvas; 

      path.lineTo(currentX, currentY); 
      canvas.drawPath(path, paint); 

     new DownloadImageTask(canvas, this) 
       .execute("http://<ip>:<port>/map/download/image?imageType=jpg&mapId=1"); 
    } 

    private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> { 
     IAsyncResponse delegate; 
     Canvas canvas; 

     public DownloadImageTask(Canvas canvas, IAsyncResponse delegate) { 
      this.canvas = canvas; 
      this.delegate = delegate; 
     } 

     protected Bitmap doInBackground(String... urls) { 
      String urldisplay = urls[0]; 
      Bitmap mIcon11 = null; 
      try { 
       InputStream in = new java.net.URL(urldisplay).openStream(); 
       mIcon11 = BitmapFactory.decodeStream(in); 
      } catch (Exception e) { 
       Log.e("Error", e.getMessage()); 
       e.printStackTrace(); 
      } 
      return mIcon11; 
     } 

     protected void onPostExecute(Bitmap result) { 
      delegate.processFinished(result); 
     } 
    } 

    @Override 
    public void processFinished(Bitmap bmp) { 
     canvas.drawBitmap(bmp, new Rect(100,100,150,150), new Rect(0,0,50,50) , null); 
    } 

} 

Répondre

1
public class MapView 
     extends View 
     implements IAsyncResponse { 

    public MapView(Context context) { 
     super(context); 
     init(); 
    } 

    public MapView(
      Context context, 
      AttributeSet attrs) { 
     super(context, attrs); 
     init(); 

    } 

    public MapView(
      Context context, 
      AttributeSet attrs, 
      int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
     init(); 
    } 

    private Path path; 
    private Paint paint; 

    private URL url; 

    private Bitmap bmp; 

    // use common init method for initialization, which is called from all the constructors 
    void init() { 
     path = new Path(); 
     paint = new Paint(); 
     paint.setStyle(Paint.Style.STROKE); 
     paint.setColor(Color.RED); 
     paint.setStrokeWidth(10); 
     path.moveTo(0, 0); 

     // start the downloader task only once and not within onDraw callback 
     // please note that, reference to canvas is not hold 
     new DownloadImageTask(this).execute("my/image/url"); 
    } 

    @Override 
    public void onDraw(Canvas canvas) { 
     // all your other drawing logic goes here 
     if (null != bmp) { 
      canvas.drawBitmap(bmp, new Rect(100, 100, 150, 150), new Rect(0, 0, 50, 50), null); 
     } 
    } 

    private class DownloadImageTask 
      extends AsyncTask<String, Void, Bitmap> { 
     IAsyncResponse delegate; 

     public DownloadImageTask(
       IAsyncResponse delegate) { 
      this.delegate = delegate; 
     } 

     protected Bitmap doInBackground(String... urls) { 
      String urldisplay = urls[0]; 
      Bitmap mIcon11 = null; 
      try { 
       InputStream in = new java.net.URL(urldisplay).openStream(); 
       mIcon11 = BitmapFactory.decodeStream(in); 
      } catch (Exception e) { 
       Log.e("Error", e.getMessage()); 
       e.printStackTrace(); 
      } 
      return mIcon11; 
     } 

     protected void onPostExecute(Bitmap result) { 
      // you probably don't even need delegate. But I have kept it for consistency. 
      // alternatively you can remove this interface and do - 
      //   MapView.this.bmp = bmp; 
      //   invalidate(); 

      delegate.processFinished(result); 
     } 
    } 

    @Override 
    public void processFinished(Bitmap bmp) { 
     this.bmp = bmp; 
     invalidate(); 
    } 

}