1

カスタム ビューを .jpg 形式で保存するために、いくつかのコードを試しました。ビューをビットマップに変換してから、以下に示すように保存しています。ただし、試行するたびに、保存される画像のサイズは常に 0 KB であり、ファイルには何も含まれていません。私はすでに次の許可を入れています。

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

これは私が使用したコードの1つです。

drawView.setDrawingCacheEnabled(true);
drawView.buildDrawingCache();
Bitmap b = drawView.getDrawingCache();
 private void SaveImage(Bitmap finalBitmap) {

      String root = Environment.getExternalStorageDirectory().toString();
      File myDir = new File(root + "/saved_images");    
      myDir.mkdirs();
      String fname = "Image.jpg";
      File file = new File (myDir, fname);
      if (file.exists ()) file.delete (); 
        try {
           FileOutputStream out = new FileOutputStream(file);
           finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
           out.flush();
           out.close();

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

また、このコードの主なアイデアは私によるものではないことを明確にしたいと思いました。

完全なコード全体; public class DrawPad extends View {

//drawing path
    private Path drawPath;
    //drawing and canvas paint
    private Paint drawPaint, canvasPaint;
    //initial color
    private int paintColor =0xff000000;
    //canvas
    private Canvas drawCanvas;
    //canvas bitmap
    private Bitmap canvasBitmap;

    //erase flag
    private boolean erase=false;

    public DrawPad(Context context, AttributeSet attrs){
        super(context, attrs);
        setupDrawing();


    }
    public Paint getPaint(){
        return drawPaint;
    }
    //setup drawing
    private void setupDrawing(){

        //prepare for drawing and setup paint stroke properties

        drawPath = new Path();
        drawPaint = new Paint();
        drawPaint.setColor(paintColor);
        drawPaint.setAntiAlias(true);
        drawPaint.setStrokeWidth(1.0f);
        drawPaint.setStyle(Paint.Style.STROKE);
        drawPaint.setStrokeJoin(Paint.Join.ROUND);
        drawPaint.setStrokeCap(Paint.Cap.ROUND);
        canvasPaint = new Paint(Paint.DITHER_FLAG);

    }
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        canvasBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        drawCanvas = new Canvas(canvasBitmap);
    }

    //draw the view - will be called after touch event
    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawBitmap(canvasBitmap, 0, 0, canvasPaint);
        canvas.drawPath(drawPath, drawPaint);
    }

    //register user touches as drawing action
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float touchX = event.getX();
        float touchY = event.getY();
        //respond to down, move and up events
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            drawPath.moveTo(touchX, touchY);
            break;
        case MotionEvent.ACTION_MOVE:
            drawPath.lineTo(touchX, touchY);
            break;
        case MotionEvent.ACTION_UP:
            drawPath.lineTo(touchX, touchY);
            drawCanvas.drawPath(drawPath, drawPaint);
            drawPath.reset();
            break;
        default:
            return false;
        }
        //redraw
        invalidate();
        return true;

    }

    //update color
    public void setColor(String newColor){
        invalidate();
        paintColor = Color.parseColor(newColor);
        drawPaint.setColor(paintColor);
    }

    //set erase true or false
    public void setErase(boolean isErase){
        erase=isErase;

        if(erase){ 
            drawPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
            drawPaint.setStrokeWidth(15.0f);
        }
        else {
            drawPaint.setStrokeWidth(1.0f);
            drawPaint.setXfermode(null);
        }
    }

    //start new drawing
    public void startNew(){
        drawCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
        drawPaint.setStrokeWidth(1.0f);
        invalidate();
    }
    public void SaveImage(Bitmap finalBitmap,String spin) {
        if(finalBitmap == null) {
            Toast unsavedToast = Toast.makeText(getContext(), 
                    "Oops! Page could not be saved.", Toast.LENGTH_SHORT);
            unsavedToast.show();
            }

          this.setDrawingCacheEnabled(true);
          this.buildDrawingCache();
          finalBitmap=this.getDrawingCache();
          String root = Environment.getExternalStorageDirectory().toString();
          File myDir = new File(root + "/answers");    
          myDir.mkdirs();
          String fname = spin+".jpg";
          File file = new File (myDir, fname);
          if (file.exists ()) file.delete (); 
            try {
               FileOutputStream out = new FileOutputStream(file);
              if ( finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out)==true){
               out.flush();
               out.close();
              }
              if(finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out)==false){
                Toast fToast = Toast.makeText(getContext(), 
                        "compression failed", Toast.LENGTH_SHORT);
                fToast.show();
              }

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

2 に答える 2