0
我正在研究一个应用程序,在该应用程序中,当用户单击按钮时,需要将表数据保存为图像。要做到这一点我用下面的代码:Android如何重置画布/位图以捕获新图像
//save is the button to click
this.save.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
AlertDialog.Builder alert=new AlertDialog.Builder(MyActivity.this);
alert.setTitle("Save");
alert.setMessage("Enter a file name");
final EditText input=new EditText(MyActivity.this);
alert.setView(input);
alert.setPositiveButton("OK", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
table.setDrawingCacheEnabled(true);
Bitmap b=table.getDrawingCache();
Bitmap combo=Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas=new Canvas(combo);
canvas.drawBitmap(b, 0f, 175f, null);
OutputStream outStream = null;
String value=input.getText().toString();
File directory =new File(extStorageDirectory+"/Files/");
if(!directory.mkdir())
directory.mkdir();
File file = new File(directory, value);
try {
outStream = new FileOutputStream(file);
combo.compress(Bitmap.CompressFormat.PNG, 100, outStream);
FileOutputStream fOut=openFileOutput("public.dat", Context.MODE_PRIVATE|Context.MODE_APPEND);
OutputStreamWriter osw=new OutputStreamWriter(fOut);
osw.write(value+"\n");
osw.flush();
osw.close();
fOut.flush();
fOut.close();
outStream.flush();
outStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
此功能可以节省没有问题的图像,但是当我改变我的表,并试图挽救另一个图像将其保存的第一个。我想我需要重新设置画布或类似的东西,所以我在保存文件后试过以下内容:
canvas.restore();
但它不起作用。但是,当我在模拟器上重新启动应用程序时,我可以保存一个新图像。有人可以帮我解决这个问题。
什么类型的对象是表?你没有指定,因为它是你的解决方案的一部分 – IcedDante 2014-02-03 15:52:04
@IcedDante它是一个带有Tablerow元素的线性布局。 – Anila 2014-02-03 18:08:50