Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/224.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 用于将绘制的视图保存到图像的源代码_Java_Android_Image_Save_Android Canvas - Fatal编程技术网

Java 用于将绘制的视图保存到图像的源代码

Java 用于将绘制的视图保存到图像的源代码,java,android,image,save,android-canvas,Java,Android,Image,Save,Android Canvas,我有一个用画布画物体的应用。我想像保存图像一样保存这些图片。例如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 A

我有一个用画布画物体的应用。我想像保存图像一样保存这些图片。例如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();

}
制作arrayList

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

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

}
我尝试过这个,但在开始按钮后,单击loadBitmapFromView(v); save(); 我的程序关闭了,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());
}

    }

要从视图中获取位图,请执行以下操作:

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());
    }
}

请告诉我们你到目前为止都做了些什么。就这样。我想有一个保存像bmp这样的绘制对象的方法。它看起来很棒,谢谢。但是我对这一行有问题:MediaStore.Images.Media.insertImage(getContentResolver(),file.getAbsolutePath(),file.getName(),file.getName());我不知道应该导入或声明什么。FileNotFoundException FileNotFoundException:MediaStore.Images.Media.insertImage(getContentResolver(),file.getAbsolutePath(),‌​file.getName(),file.getName());文件路径应该有问题。添加一些日志以检查结果文件和fOut,并在此处发布日志。我将在回复中为您编辑函数。现在我在这一行有错误:Log.d(“saveImage”,“无法打开文件:”,e.getMessage());描述信息是:“日志类型中的方法D(String,String,Throwable)不适用于参数(String,String,String)”,所以如果我修改为:“Log.D(“saveImage”,“无法打开文件:”);“我得到它:最后三行代码的未处理异常类型IOException”(不带“{”)
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());
}
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());
    }
}