本文介绍了如何保存图像从画布code在Android应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经创建了的OnDraw(Canvas)的一个形象,我试图以保存图像,但它不是Working.image保存有大小为零。在这里,我的编码
私人诠释瓦;
私人诠释H;
@覆盖保护无效onSizeChanged(INT W,INT小时,INT oldw,INT oldh)
{
this.w = W;
this.h = H;
super.onSizeChanged(W,H,oldw,oldh);
}
@覆盖
保护无效的OnDraw(帆布油画)
{
// TODO自动生成方法存根
位图toDisk = Bitmap.createBitmap(W,H,Bitmap.Config.ARGB_8888);
canvas.setBitmap(toDisk);
涂料粉刷=新的油漆();
位图MYBITMAP = BitmapFactory.de codeResource(getResources(),R.drawable.ban_background);
位图resizeImage1 = Bitmap.createScaledBitmap(MYBITMAP,590350,假);
canvas.drawColor(Color.BLACK);
canvas.drawBitmap(resizeImage1,10,5,NULL);
paint.setStyle(Paint.Style.FILL);
paint.setAntiAlias(真正的);
paint.setTextSize(25);
paint.setColor(Color.BLUE);
paint.setFakeBoldText(真正的);
位图的图标;
位图ICON1;
位图图标2;
尝试{//获取参考AssetManager
AssetManager mngr = getAssets();
//创建一个输入流,资产文件夹阅读
InputStream的插件= mngr.open(imageName +PNG);
图标= BitmapFactory.de codeStream(INS);
位图icon9 = Bitmap.createScaledBitmap(图标,590250,假);
canvas.drawBitmap(icon9,1,60,NULL);
canvas.save();
如果(isCamera)
{
ICON1 = BitmapFactory.de codeByteArray(pictureByte,0,pictureByte.length);
位图resizeImage = Bitmap.createScaledBitmap(icon1,150,170,假);
canvas.drawBitmap(resizeImage,400100,NULL);
canvas.save();
}
如果(isPhotoGallery)
{
位图myBitmap1 = BitmapFactory.de codeFILE(selectedImagePath);
位图resizeImage = Bitmap.createScaledBitmap(myBitmap1,150,170,假);
canvas.drawBitmap(resizeImage,400100,NULL);
}
如果(isImageSearch)
{
图标2 = BitmapFactory.de codeByteArray(pictureByte,0,pictureByte.length);
canvas.drawBitmap(icon2,10,10,NULL);
}
paint.setTextSize(30);
canvas.drawText(CameraText,100175,油漆);
getDrawingCache()的COM preSS(Bitmap.Com pressFormat.JPEG,100,新的FileOutputStream(新文件(到/ mnt / SD卡/ arun.jpg)))。
}赶上(例外五){
Log.e(错误--------->中,e.toString());
}
//位图toDisk = Bitmap.createBitmap(resizeImage1);
//小号canvas.setBitmap(toDisk); / * code ... * /
尝试 {
toDisk.com preSS(Bitmap.Com pressFormat.JPEG,100,新的FileOutputStream(新文件(到/ mnt / SD卡/ arun.jpg)));
}赶上(FileNotFoundException异常E){
// TODO自动生成的catch块
e.printStackTrace();
}
解决方案
试试这个:
私人诠释瓦;
私人诠释H;
@覆盖
保护无效onSizeChanged(INT W,INT小时,INT oldw,诠释oldh){
this.w = W;
this.h = H;
super.onSizeChanged(W,H,oldw,oldh);
}
保护无效的OnDraw(帆布油画)
{
位图toDisk = Bitmap.createBitmap(W,H,Bitmap.Config.ARGB_8888);
canvas.setBitmap(toDisk);
/ * code ... * /
toDisk.com preSS(Bitmap.Com pressFormat.JPEG,100,新的FileOutputStream(新文件(到/ mnt / SD卡/ arun.jpg)));
/ * code ... * /
}
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();
}
解决方案
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... */
}
这篇关于如何保存图像从画布code在Android应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!