Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have created a image in OnDraw(Canvas),i tried to Save that Image But its Not Working.image is Saved with Zero size. Here my coding

 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();
                } 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
180 views
Welcome To Ask or Share your Answers For Others

1 Answer

Try this:

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... */
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...