I am working on an application in which i need to save table data as an image when the user clicks on a button. To do so I used the following code:
//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();
}
}
});
This function saves an image with no problem but when i change my table and try to save another image it saves the first one. I think i need to reset the canvas or something like that so I tried the following after saving the file:
canvas.restore();
But it doesn’t work. But wheh i relaunch the application on the simulator i can save a new image. Can someone please help me solve this problem.
I found the solution to my problem. I just had to add
just before catching exceptions.