Android画布位图,我保存的图像是黑色的

Android画布位图,我保存的图像是黑色的,android,canvas,bitmap,Android,Canvas,Bitmap,对不起,我的英语不好,如果你不懂,请告诉我,我会尽量解释得更好 你好,我正在做一个测试,我想先用画布和表面视图绘制一条线,然后我想把它保存在我的智能手机上。。。我尝试了这段代码,但它只保存了黑色图像。你知道为什么吗 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.salvaaa);

对不起,我的英语不好,如果你不懂,请告诉我,我会尽量解释得更好 你好,我正在做一个测试,我想先用画布和表面视图绘制一条线,然后我想把它保存在我的智能手机上。。。我尝试了这段代码,但它只保存了黑色图像。你知道为什么吗

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.salvaaa);


        SurfaceView surface = (SurfaceView) findViewById(R.id.surface);
        surface.getHolder().addCallback(new Callback() {

            private Context context;

            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                Paint paint = new Paint();
                paint.setColor(Color.WHITE);
                Canvas canvas = holder.lockCanvas();
                canvas.drawColor(Color.WHITE);
                   canvas.drawLine(20,20,30,30, paint);
                   holder.unlockCanvasAndPost(canvas);
                   Bitmap bitmap = Bitmap.createBitmap(400, 400, Bitmap.Config.ARGB_8888);
                   canvas.setBitmap(bitmap);


                     Context context = getBaseContext();
                   this.context=context;
                    String path = Environment.getExternalStorageDirectory().toString();
                    OutputStream fOut=null;
                    File file = new File(path, "immasine.jpg");

                    try {
                        fOut= new FileOutputStream(file);
                        bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
                        fOut.flush();
                        fOut.close();
                        MediaStore.Images.Media.insertImage(context.getContentResolver(), file.getAbsolutePath(), file.getName(), file.getName());

                    } catch (FileNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }




            }

          @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

            } });
    }
编辑------------

我已经更改了代码,现在它可以工作了

super.onCreate(savedInstanceState);
    setContentView(R.layout.salvaaa);
          Bitmap bitmap = Bitmap.createBitmap(400, 400, Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(bitmap);
            Paint paint = new Paint();
            paint.setColor(Color.BLACK);
            canvas.drawColor(Color.WHITE);
              canvas.drawLine(20,20,30,30, paint);
              OutputStream fOut=null;
                File file = new File(getFilesDir(), "isne");

                try {
                    fOut= new FileOutputStream(file);
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
                    fOut.flush();
                    fOut.close();
                    MediaStore.Images.Media.insertImage(this.getContentResolver(), file.getAbsolutePath(), file.getName(), file.getName());
                   // holder.unlockCanvasAndPost(canvas);
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

我看到的一件事是,您正在将其保存为jpg,但压缩格式为png:Bitmap.CompressFormat.png“imasine.jpg”是名称,因此它不会更改。。。但是我尝试过更改它,但没有任何更改。您已经将Bitmap.CompressFormat.PNG更改为Bitmap.CompressFormat.JPG?是的,现在我注意到,即使我将“.PNG”放在jpeg格式中,它也会将图像保存为jpeg格式…当然会,因为名称是“imasine.JPG”,但CompressFormat是PNG。但让我恼火的是:holder.unlockCanvasAndPost(canvas);-API上写着:“调用后,曲面的当前像素将显示在屏幕上,但其内容丢失,特别是无法保证曲面的内容将保留”……您能在不调用的情况下尝试吗?结果如何?