如何在android中保存签名?
您好,我已经创建了一个面板,可以在面板上绘制签名。当我单击“保存”按钮时,签名必须保存在文件中。我尝试了以下编码,但无法保存文件。我在logcat中遇到异常如何在android中保存签名?,android,Android,您好,我已经创建了一个面板,可以在面板上绘制签名。当我单击“保存”按钮时,签名必须保存在文件中。我尝试了以下编码,但无法保存文件。我在logcat中遇到异常 public class Signatures_saveActivity extends Activity { LinearLayout mContent; signature mSignature; Button mClear, mGetSign; public static String tempDir
public class Signatures_saveActivity extends Activity {
LinearLayout mContent;
signature mSignature;
Button mClear, mGetSign;
public static String tempDir;
public int count = 1;
public String current = null;
private Bitmap mBitmap;
View mView;
String ss;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tempDir = Environment.getExternalStorageDirectory() + "/"
+ getResources().getString(R.string.external_dir) + "/";
prepareDirectory();
current = count + ".png";
mContent = (LinearLayout) findViewById(R.id.linearLayout);
mSignature = new signature(this, null);
mSignature.setBackgroundColor(Color.BLUE);
mContent.addView(mSignature, LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT);
mClear = (Button) findViewById(R.id.clear);
mGetSign = (Button) findViewById(R.id.getsign);
mView = mContent;
mClear.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Log.v("log_tag", "Panel Cleared");
mSignature.clear();
}
});
mGetSign.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Log.v("log_tag", "Panel Saved");
mView.setDrawingCacheEnabled(true);
mSignature.save(mView);
}
});
}
private boolean prepareDirectory() {
try {
if (makedirs()) {
return true;
} else {
return false;
}
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(
this,
"Could not initiate File System.. Is Sdcard mounted properly?",
1000).show();
return false;
}
}
private boolean makedirs() {
File tempdir = new File(tempDir);
if (!tempdir.exists())
tempdir.mkdirs();
if (tempdir.isDirectory()) {
File[] files = tempdir.listFiles();
for (File file : files) {
if (!file.delete()) {
System.out.println("Failed to delete " + file);
}
}
}
return (tempdir.isDirectory());
}
public class signature extends View {
private static final float STROKE_WIDTH = 5f;
private static final float HALF_STROKE_WIDTH = STROKE_WIDTH / 2;
private Paint paint = new Paint();
private Path path = new Path();
private float lastTouchX;
private float lastTouchY;
private final RectF dirtyRect = new RectF();
public signature(Context context, AttributeSet attrs) {
super(context, attrs);
paint.setAntiAlias(true);
paint.setColor(Color.YELLOW);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeWidth(STROKE_WIDTH);
}
public void save(View v) {
Log.v("log_tag", "Width: " + v.getWidth());
Log.v("log_tag", "Height: " + v.getHeight());
if (mBitmap == null) {
mBitmap = Bitmap.createBitmap(320, 480, Bitmap.Config.RGB_565);
}
Canvas canvas = new Canvas(mBitmap);
String FtoSave = tempDir + current;
File file = new File(FtoSave);
try {
FileOutputStream mFileOutStream = new FileOutputStream(file);
v.draw(canvas);
mBitmap.compress(Bitmap.CompressFormat.PNG, 90, mFileOutStream);
// Bitmap bmp = intent.getExtras().get("data");
// ByteArrayOutputStream stream = new ByteArrayOutputStream();
// mBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
// byte[] val = stream.toByteArray();
// String s = new String(val.toString());
//
// ss = Base64.encodeToString(val, Base64.DEFAULT);
// System.out.println("String image data" + ss);
mFileOutStream.flush();
mFileOutStream.close();
String url = Images.Media.insertImage(getContentResolver(),
mBitmap, "title", null);
Log.v("log_tag", "url" + url);
} catch (Exception e) {
Log.v("log_tag", e.toString());
}
}
public void clear() {
path.reset();
invalidate();
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawPath(path, paint);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float eventX = event.getX();
float eventY = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
path.moveTo(eventX, eventY);
lastTouchX = eventX;
lastTouchY = eventY;
return true;
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_UP:
resetDirtyRect(eventX, eventY);
int historySize = event.getHistorySize();
for (int i = 0; i < historySize; i++) {
float historicalX = event.getHistoricalX(i);
float historicalY = event.getHistoricalY(i);
expandDirtyRect(historicalX, historicalY);
path.lineTo(historicalX, historicalY);
}
path.lineTo(eventX, eventY);
break;
default:
debug("Ignored touch event: " + event.toString());
return false;
}
invalidate((int) (dirtyRect.left - HALF_STROKE_WIDTH),
(int) (dirtyRect.top - HALF_STROKE_WIDTH),
(int) (dirtyRect.right + HALF_STROKE_WIDTH),
(int) (dirtyRect.bottom + HALF_STROKE_WIDTH));
lastTouchX = eventX;
lastTouchY = eventY;
return true;
}
private void debug(String string) {
}
private void expandDirtyRect(float historicalX, float historicalY) {
if (historicalX < dirtyRect.left) {
dirtyRect.left = historicalX;
} else if (historicalX > dirtyRect.right) {
dirtyRect.right = historicalX;
}
if (historicalY < dirtyRect.top) {
dirtyRect.top = historicalY;
} else if (historicalY > dirtyRect.bottom) {
dirtyRect.bottom = historicalY;
}
}
private void resetDirtyRect(float eventX, float eventY) {
dirtyRect.left = Math.min(lastTouchX, eventX);
dirtyRect.right = Math.max(lastTouchX, eventX);
dirtyRect.top = Math.min(lastTouchY, eventY);
dirtyRect.bottom = Math.max(lastTouchY, eventY);
}
}
}
公共类签名\u saveActivity扩展活动{
线性布局内容;
签名;
按钮mClear、mGetSign;
公共静态字符串tempDir;
公共整数计数=1;
公共字符串current=null;
私有位图mBitmap;
视图视图;
字符串ss;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tempDir=Environment.getExternalStorageDirectory()+“/”
+getResources().getString(R.string.external_dir)+“/”;
prepareDirectory();
当前=计数+“.png”;
mContent=(LinearLayout)findViewById(R.id.LinearLayout);
mSignature=新签名(此为空);
mSignature.setBackgroundColor(Color.BLUE);
mContent.addView(mSignature、LayoutParams.FILL\u父级、,
LayoutParams.FILL\u PARENT);
mClear=(按钮)findviewbyd(R.id.clear);
mGetSign=(按钮)findViewById(R.id.getsign);
mView=mContent;
setOnClickListener(新的OnClickListener(){
公共void onClick(视图v){
Log.v(“Log_标签”,“面板清除”);
mSignature.clear();
}
});
mGetSign.setOnClickListener(新的OnClickListener(){
公共void onClick(视图v){
Log.v(“日志标签”、“面板已保存”);
mView.setDrawingCacheEnabled(true);
mSignature.save(mView);
}
});
}
私有布尔prepareDirectory(){
试一试{
if(makedirs()){
返回true;
}否则{
返回false;
}
}捕获(例外e){
e、 printStackTrace();
Toast.makeText(
这
“无法启动文件系统..SD卡安装是否正确?”,
1000)show();
返回false;
}
}
私有布尔makedirs(){
文件tempdir=新文件(tempdir);
如果(!tempdir.exists())
tempdir.mkdirs();
if(tempdir.isDirectory()){
File[]files=tempdir.listFiles();
用于(文件:文件){
如果(!file.delete()){
System.out.println(“删除”+文件失败);
}
}
}
return(tempdir.isDirectory());
}
公共类签名扩展视图{
专用静态最终浮动行程_宽度=5f;
专用静态最终浮动半冲程宽度=冲程宽度/2;
私人油漆=新油漆();
私有路径路径=新路径();
私人浮动lastTouchX;
私密接触;
private final RectF dirtyRect=new RectF();
公共签名(上下文、属性集属性){
超级(上下文,attrs);
paint.setAntiAlias(真);
油漆。设置颜色(颜色。黄色);
绘制.设置样式(绘制.样式.笔划);
绘制.设置行程连接(绘制.连接.圆形);
油漆。设置行程宽度(行程宽度);
}
公共作废保存(视图v){
Log.v(“Log_标签”,“宽度:+v.getWidth());
Log.v(“Log_标签”,“高度:+v.getHeight());
if(mBitmap==null){
mBitmap=Bitmap.createBitmap(320480,Bitmap.Config.RGB_565);
}
画布画布=新画布(mBitmap);
字符串FtoSave=tempDir+current;
文件文件=新文件(FtoSave);
试一试{
FileOutputStream mFileOutStream=新的FileOutputStream(文件);
v、 绘画(画布);
mBitmap.compress(Bitmap.CompressFormat.PNG,90,mFileOutStream);
//位图bmp=intent.getExtras().get(“数据”);
//ByteArrayOutputStream=新建ByteArrayOutputStream();
//mBitmap.compress(Bitmap.CompressFormat.PNG,100,流);
//字节[]val=stream.toByteArray();
//字符串s=新字符串(val.toString());
//
//ss=Base64.encodeToString(val,Base64.DEFAULT);
//System.out.println(“字符串图像数据”+ss);
mFileOutStream.flush();
mFileOutStream.close();
字符串url=Images.Media.insertImage(getContentResolver(),
mBitmap,“title”,null);
Log.v(“日志标签”、“url”+url);
}捕获(例外e){
Log.v(“Log_标记”,例如toString());
}
}
公共空间清除(){
path.reset();
使无效();
}
@凌驾
受保护的void onDraw(画布){
画布.绘制路径(路径,绘制);
}
@凌驾
公共布尔onTouchEvent(运动事件){
float eventX=event.getX();
float eventY=event.getY();
开关(event.getAction()){
case MotionEvent.ACTION\u DOWN:
path.moveTo(eventX,eventY);
lastTouchX=eventX;
lastTouchY=eventY;
返回true;
case MotionEvent.ACTION\u移动:
case MotionEvent.ACTION\u UP:
resetDirtyRect(eventX,eventY);
int historySize=event.getHistorySize();
for(int i=0;i