-1

我有一个应用程序,我通过画布绘制对象。我想像图片一样保存这些图片。例如 bmp 或 jpg 到文件夹。我该怎么做 ?这是我的代码。

public class hell extends View{


public static int width= aktivita.width;    
public static int koeficient = 5;

final Paint mPaint;

public static int t;
public boolean filter = true;
static ArrayList<Circle> mCircles;


private static boolean Kontroler = true;





public void draw(Canvas canvas) {

     Paint p = new Paint();

     p.setColor(Color.RED);


            kres(canvas);


            invalidate();
        }
    }




public krouzky(Context context, AttributeSet atrs) {
    super(context, atrs);

    mMalovani = new Paint();
    mMalovani.setColor(Color.RED);
    mMalovani.setAntiAlias(true);

        createCircles();

}

添加绘图对象

public static void Prid() {

     int ran = aktivita.width/8;

    mCircles.add(new Circle(80, 200, ran));

}

制作数组列表

private static void createCircles() { if (mCircles == null) { mCircles = new ArrayList<Circle>(); }

 int r = aktivita.width/8;
mCircles.add(new Circle(80, 200, r));

}

绘图对象 private void kres(Canvas canvas) {

        for (Circle c : mCircles) {
            // 
            canvas.drawCircle(c.getCurrentX(), c.getCurrentY(), c.getRadius(),
                    mPaint);


        }
        }

我试过这个,但在按钮开始后点击 loadBitmapFromView(v); 节省(); 我的程序关闭了,并且在 SD 卡中是带有 .png 后缀的文件。但它有 0 口,我没有打开它。

 public static Bitmap loadBitmapFromView(View view) {

        // width measure spec 
        int widthSpec = View.MeasureSpec.makeMeasureSpec(
                view.getMeasuredWidth(), View.MeasureSpec.AT_MOST);
        // height measure spec 
        int heightSpec = View.MeasureSpec.makeMeasureSpec(
                view.getMeasuredHeight(), View.MeasureSpec.AT_MOST);
        // measure the view 
        view.measure(widthSpec, heightSpec);
        // set the layout sizes 
        view.layout(view.getLeft(), view.getTop(), view.getMeasuredWidth() + view.getLeft(), view.getMeasuredHeight() + view.getTop());
        // create the bitmap
        Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
        // create a canvas used to get the view's image and draw it on the bitmap 
        Canvas c = new Canvas(bitmap);
        // position the image inside the canvas 
        c.translate(-view.getScrollX(), -view.getScrollY());
        // get the canvas
        view.draw(c);

        return bitmap;
}

    public void save(){
 String fileName = String.valueOf(Calendar.getInstance().getTimeInMillis());
// generate the image path 
String imagePath = Environment.getExternalStorageDirectory().toString() + File.separator +  fileName + ".png";

 try {

     // save the image as png 
    FileOutputStream out = new FileOutputStream(imagePath);
    View view = null;
    // compress the image to png and pass it to the output stream 
    loadBitmapFromView(view).compress(Bitmap.CompressFormat.PNG, 90, out);

   // save the image 
   out.flush();
   out.close();

} catch (Exception error) {
      Log.e("Error saving image", error.getMessage());
}

    }
4

1 回答 1

1

从视图中获取位图:

public static Bitmap getBitmapFromView(View view) {
    Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(returnedBitmap);
    Drawable bgDrawable =view.getBackground();
    if (bgDrawable!=null) 
        bgDrawable.draw(canvas);
    else 
        canvas.drawColor(Color.WHITE);
    view.draw(canvas);
    return returnedBitmap;
}

要将其保存到 SD 卡:

public void saveImage(Bitmap b)
{
    String path = Environment.getExternalStorageDirectory().toString();
    OutputStream fOut = null;

    File file = new File(path, "XXXXXXXXXXX.jpg");
    fOut = new FileOutputStream(file);

    b.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
    fOut.flush();
    fOut.close();
    MediaStore.Images.Media.insertImage(getContentResolver(),file.getAbsolutePath(),file.getName(),file.getName());
}

您可以只在 MediaStore 中进行插入,但它会降低质量。这样压缩就可以达到您想要的质量。

编辑:

添加了一些日志记录以帮助找出您收到 FileNotFound 异常的原因:

public void saveImage(Bitmap b)
{
    String path = Environment.getExternalStorageDirectory().toString();
    OutputStream fOut = null;

    File file = new File(path, "XXXXXXXXXXX.jpg");
    Log.d("saveImage", "File to save: " + file.getAbsolutePath());

    try {
        FileOutputStream fOut = new FileOutputStream(file);
    }
    catch(FileNotFoundException e) {
        Log.d("saveImage", "Couldn't open file: " + e.getMessage());
    }

    if(fOut != null) {
        b.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
        fOut.flush();
        fOut.close();
        MediaStore.Images.Media.insertImage(getContentResolver(),file.getAbsolutePath(),file.getName(),file.getName());
    }
}
于 2013-08-27T17:07:39.880 回答