[英]How to: Save the canvas as image on SD card and always keeps the first picture
I'm creating a android app that has the following purpose: 我正在创建一个具有以下目的的android应用程序:
Code: 码:
Button Colorpaint = (Button) findViewById(R.id.color);
Colorpaint.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
int _color = R.color.red;
new PickerDialog(v.getContext(),new OnColorChangedListener() {
public void colorChanged(int color) {
mPaint.setColor(color);
Log.i("TAG", "mpaint one" +mPaint);
}
}, mPaint.getColor(), _color).show();
Log.i("TAG", "mpaint two" +mPaint);
}
});
ClearPaint = (Button) findViewById(R.id.ClearPaint);
ClearPaint.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mBitmap.eraseColor(Color.TRANSPARENT);
mPath.reset();
mView.invalidate();
}
});
btn_shoot = (Button)findViewById(R.id.btn_shoot);
btn_shoot.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
View view = findViewById(R.id.item);
view.setDrawingCacheEnabled(true);
Bitmap bitmap = view.getDrawingCache();
BitmapDrawable bitmapDrawable = new BitmapDrawable(bitmap);
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
//we check if external storage is available, otherwise display an error message to the user
File sdCard = Environment.getExternalStorageDirectory();
File directory = new File (sdCard.getAbsolutePath() + "/Basketball_Coach_Board");
directory.mkdirs();
String filename = "tactics" + i + ".jpg";
File yourFile = new File(directory, filename);
while (yourFile.exists()) {
i++;
filename = "tactics" + i + ".jpg";
yourFile = new File(directory, filename);
}
if (!yourFile.exists()) {
if (directory.canWrite())
{
try {
FileOutputStream out = new FileOutputStream(yourFile, true);
bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
out.flush();
out.close();
Toast.makeText(MainActivity.this, "Tactics saved at /sdcard/Basketball_Coach_Board/tactics" + i + ".jpg", Toast.LENGTH_SHORT).show();
i++;
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
else
{
Toast.makeText(MainActivity.this, "SD Card not available!", Toast.LENGTH_SHORT).show();
}
}
});
我猜这是因为成功拍照之后,您不会通过以下方式将绘图缓存重置为false: view.setDrawingCacheEnabled(false);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.