如何保存图片到文件Android的自定义视图? [英] how to save image to file android custom view?
问题描述
我有一个问题。
我尝试保存的画布图像文件
这是我的工作方式。
位图1.创建画布对象(表图)
画布2.打印文本(使用的drawText)
3.save与bitmap.com preSS()方法
但它只能保存原始位图(带出来的文字)
我不知道我能救文印的位图到文件?
这里是我的code
保护类MyView的扩展视图{ 公共MyView的(上下文的背景下){
超级(上下文);
} 公共无效的onDraw(帆布油画){
涂料PNT =新的油漆();
pnt.setColor(Color.BLACK);
canvas.scale(0.5F,0.5F);
资源解析度= getResources();
BitmapDrawable BD =(BitmapDrawable)res.getDrawable(R.drawable.hanhwa);
位图位= bd.getBitmap();
canvas.drawBitmap(位0,0,NULL); pnt.setTextSize(30);
canvas.drawText(你好,290340,PNT);
canvas.restore();
//保存文件//
文件夹=新的文件(Environment.getExternalStorageDirectory()+/ DCIM / tmp目录); 布尔ISEXIST =真;
如果(!folder.exists()){
ISEXIST = folder.mkdir();
}
如果(ISEXIST){
文件file = NULL;
布尔isFileExist = FALSE;
文件=新的文件(folder.getPath()+/tmp.jpg); 如果(文件= NULL&放大器;!&安培;!file.exists()){
尝试{
isFileExist = file.createNewFile();
}赶上(IOException异常五){
e.printStackTrace();
}
}
其他{
} 如果(file.exists()){
FOS的FileOutputStream = NULL;
尝试{
FOS =新的FileOutputStream(文件); bit.com preSS(Bitmap.Com pressFormat.JPEG,100,FOS);
}
赶上(例外五){
e.printStackTrace();
}
最后{
尝试{
fos.close();
}
赶上(IOException异常五){
e.printStackTrace();
}
} }
}
其他{
//Toast.makeText(MyView.this,\"foler不存在,Toast.LENGTH_LONG).show();
}
}
}
Bitmap.com preSSS()
这方法写的位图的COM pressed版本指定的OutputStream。
传位图实例帆布
下面是伪code。
(除了其他报表,恩。的try-catch,语句)
- 画布C =新的Canvas(位);
- c.drawText(你好,290340,PNT);
- b.com preSS(Bitmap.Com pressFormat.JPEG,100,FOS);
<一个href=\"http://stackoverflow.com/questions/4013725/converting-a-canvas-into-bitmap-image-in-android\">this帖子会有所帮助了。
I have a question.
I try to save canvas image to file
here is how i work. 1.Create canvas object with bitmap(form image) 2.print Text on canvas (Using drawText) 3.save with bitmap.compress() method
but it saved only original bitmap(with out text)
I wonder how I can save text printed bitmap to file? Here is my code
protected class MyView extends View{
public MyView(Context context){
super(context);
}
public void onDraw(Canvas canvas){
Paint pnt = new Paint();
pnt.setColor(Color.BLACK);
canvas.scale(0.5f,0.5f);
Resources res = getResources();
BitmapDrawable bd =(BitmapDrawable)res.getDrawable(R.drawable.hanhwa);
Bitmap bit = bd.getBitmap();
canvas.drawBitmap(bit,0,0,null);
pnt.setTextSize(30);
canvas.drawText("hello",290,340,pnt);
canvas.restore();
//file save//
File folder = new File(Environment.getExternalStorageDirectory()+"/DCIM/tmp");
boolean isExist = true;
if(!folder.exists()){
isExist = folder.mkdir();
}
if(isExist){
File file = null;
boolean isFileExist = false ;
file = new File(folder.getPath() + "/tmp.jpg");
if(file != null && !file.exists()){
try{
isFileExist = file.createNewFile();
} catch(IOException e){
e.printStackTrace();
}
}
else{
}
if(file.exists()){
FileOutputStream fos = null;
try{
fos = new FileOutputStream(file);
bit.compress(Bitmap.CompressFormat.JPEG,100,fos);
}
catch(Exception e){
e.printStackTrace();
}
finally{
try{
fos.close();
}
catch(IOException e){
e.printStackTrace();
}
}
}
}
else{
//Toast.makeText(MyView.this,"foler not exist.",Toast.LENGTH_LONG).show();
}
}
}
Bitmap.compresss()
this method write a compressed version of the bitmap to the specified outputstream.
Pass bitmap instance to canvas
Here is the pseudo-code. (except other statement, ex. try-catch, statement)
- Canvas c = new Canvas(bit);
- c.drawText("hello",290,340,pnt);
- b.compress(Bitmap.CompressFormat.JPEG, 100, fos);
this post will be helpful too.
这篇关于如何保存图片到文件Android的自定义视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!