【问题标题】:How to Save The Image From Canvas code in Android Application?如何在 Android 应用程序中从 Canvas 代码中保存图像?
【发布时间】:2011-09-16 18:45:37
【问题描述】:

我在 OnDraw(Canvas) 中创建了一个图像,我尝试保存该图像,但它不工作。图像以零大小保存。 这是我的编码

 private int w;
private int h;
@Override protected void onSizeChanged(int w, int h, int oldw, int oldh)
{
    this.w = w;
    this.h = h;
    super.onSizeChanged(w, h, oldw, oldh); 
}

@Override
protected void onDraw(Canvas canvas)
{
                // TODO Auto-generated method stub
    Bitmap toDisk = Bitmap.createBitmap(w,h,Bitmap.Config.ARGB_8888); 
    canvas.setBitmap(toDisk); 

                Paint paint = new Paint();

                Bitmap myBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.ban_background);
                Bitmap resizeImage1=Bitmap.createScaledBitmap(myBitmap,590,350,false);
                canvas.drawColor(Color.BLACK);
                canvas.drawBitmap(resizeImage1,10,5, null);
                paint.setStyle(Paint.Style.FILL);
                paint.setAntiAlias(true);
                paint.setTextSize(25);
                paint.setColor(Color.BLUE);
                paint.setFakeBoldText(true);

                Bitmap icon;
                Bitmap icon1;
                Bitmap icon2;

                try { // Get reference to AssetManager
                AssetManager mngr = getAssets();

                         // Create an input stream to read from the asset folder
                         InputStream ins = mngr.open(imageName+".png");
                         icon = BitmapFactory.decodeStream(ins);
                         Bitmap icon9=Bitmap.createScaledBitmap(icon,590,250,false);
                         canvas.drawBitmap(icon9,1,60, null);
                         canvas.save();


                if(isCamera)
                {

                icon1 = BitmapFactory.decodeByteArray(pictureByte, 0,pictureByte.length);
                Bitmap resizeImage=Bitmap.createScaledBitmap(icon1,150,170,false);
                canvas.drawBitmap(resizeImage,400,100, null);
                canvas.save();
                }


                if(isPhotoGallery)
                {
                    Bitmap myBitmap1 = BitmapFactory.decodeFile(selectedImagePath);
                    Bitmap resizeImage=Bitmap.createScaledBitmap(myBitmap1,150,170,false);
                    canvas.drawBitmap(resizeImage,400,100, null);
                }
                if(isImageSearch)
                {
                    icon2= BitmapFactory.decodeByteArray(pictureByte, 0,pictureByte.length);
                    canvas.drawBitmap(icon2,10,10, null);
                }
                paint.setTextSize(30);
                canvas.drawText(CameraText, 100,175, paint);

                   getDrawingCache().compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(new File("/mnt/sdcard/arun.jpg")));
                } catch (Exception e) {
                    Log.e("Error--------->", e.toString());
                }
               // Bitmap toDisk = Bitmap.createBitmap(resizeImage1);  
               //s canvas.setBitmap(toDisk);      /* code... */    
                try {
                    toDisk.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(new File("/mnt/sdcard/arun.jpg")));
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } 

【问题讨论】:

标签: java android android-canvas


【解决方案1】:

试试这个:

private int w;
private int h;

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    this.w = w;
    this.h = h;
    super.onSizeChanged(w, h, oldw, oldh);
}


protected void onDraw(Canvas canvas)
{
    Bitmap toDisk = Bitmap.createBitmap(w,h,Bitmap.Config.ARGB_8888);
    canvas.setBitmap(toDisk);

    /* code... */

    toDisk.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(new File("/mnt/sdcard/arun.jpg")));

    /* code... */
}

【讨论】:

  • 传入的参数是什么(/*合适的参数*/)
  • 先生在我的 onDraw() 中,我有添加许多图像但在这里我只能传递一个参数。我怎样才能拍摄所有照片(最终照片)?
  • +1 谢谢先生它的工作。但我只能查看一张图片..我如何查看所有图片先生
  • 我更新了代码。画布应该画成位图,所以我猜你画的所有图片也会被画到toDisk
  • 屏幕上什么都没有显示。
猜你喜欢
  • 2020-04-12
  • 1970-01-01
  • 2016-06-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多