Android Canvas Bitmap,我保存的图像为黑色

时间:2014-06-04 14:06:27

标签: android canvas bitmap

抱歉我的英语不好,如果你不明白的话告诉我,我试着更好地解释一下 你好,我正在做一个测试,我想首先使用画布绘制和一个surfaceview一行,然后我想将它保存在我的智能手机上...我尝试这个代码,但它只保存黑色图像..你知道为什么吗?

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) {

            } });
    }

EDIT ------------

我已经更改了代码,现在可以使用了

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

0 个答案:

没有答案