如何在 Android 应用程序中保存 Canvas 代码中的图像?
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个:
Try this: