如何在 Android 应用程序中保存 Canvas 代码中的图像?

发布于 2024-12-05 02:16:20 字数 3326 浏览 0 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

﹉夏雨初晴づ 2024-12-12 02:16:20

试试这个:

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

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... */
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文