private void saveImg(View v, String name) {
Bitmap bitmap = getBitmap(v);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
File file = new File(name);
if (file.exists()) {
file.delete();
} else {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
fos.write(stream.toByteArray());
fos.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private Bitmap getBitmap(View v) {
Bitmap bitmap = Bitmap.createBitmap(v.getWidth(), v.getHeight(),
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
v.draw(canvas);
return bitmap;
}
@Override
public void onClick(View arg0) {
saveImg(iv, Environment.getExternalStorageDirectory().getPath()
+ File.separatorChar + "view.png");
}