0
我在android中创建一个应用程序,用户通过触摸屏幕创建一个图形,然后将其保存到存储。Android:如何在android编程中截图然后保存到外部存储?
我的问题是,有时目录不会创建,有时图像会保存,但有很多图像保存在黑色图像的目录中。
主要文件保存操作发生在getDrawnMessage()方法中。
这里是我的代码
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.os.Environment;
import android.view.MotionEvent;
import android.view.View;
public class DrawWrite extends View {
float TouchXD = 0, TouchYD = 0, TouchXU = 0, TouchYU = 0, TouchXM = 0,
TouchYM = 0; // Define touch co-ordinates
float x1 = 0, y1 = 0, x2 = 0, y2 = 0; // Define drawing path co-ordinates
float stroke = 2; // Define the message structure width
int i=0;
boolean Move = false, moveD = false, moveU = false; // Define whether the
// touch has occurred or
// not
boolean exp = false;
Paint paint = new Paint(); // Paint object
Path mPath = new Path(); // Define the drawing message path
Context context;
public DrawWrite(Context context) {
super(context);
this.context=context;
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
invalidate();
paint.setAntiAlias(true);
if (DrawWriteActivity.clearScreen){
mPath.reset();
DrawWriteActivity.clearScreen = false;
}
cleanButton(canvas);
try {
getDrawnMessage(canvas);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
paint.setColor(Color.parseColor(DrawWriteActivity.colorProvider));
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(DrawWriteActivity.strokeProvider);
if (moveD == true) {
x1 = TouchXD;
y1 = TouchYD;
moveD = false;
} else if (Move == true) {
x2 = TouchXD;
y2 = TouchYD;
mPath.moveTo(x1, y1);
mPath.lineTo(x2, y2);
canvas.drawPath(mPath, paint);
x1 = x2;
y1 = y2;
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
TouchXD = (float) event.getX();
TouchYD = (float) event.getY();
moveD = true;
break;
case MotionEvent.ACTION_UP:
TouchXU = (float) event.getX();
TouchYU = (float) event.getY();
moveU = true;
break;
case MotionEvent.ACTION_MOVE:
TouchXD = (float) event.getX();
TouchYD = (float) event.getY();
Move = true;
break;
}
return true;
}
public void getDrawnMessage(Canvas canvas) throws FileNotFoundException{
if(exp){
Bitmap bitmap = null;
String root = Environment.getExternalStorageDirectory().toString();
File imgDir = new File(root+"/ChitBak/");
String imgName;
bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Config.ARGB_8888);
imgDir.mkdirs();
imgName = "img"+i+".jpg";
i++;
File file = new File(imgDir,imgName);
if(file.exists()) file.delete();
FileOutputStream outImg = new FileOutputStream(file);
bitmap.compress(CompressFormat.JPEG, 100, outImg);
exp = false;
Intent in = new Intent(context, MainActivity.class);
in.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
context.startActivity(in);
}
}
private void cleanButton(Canvas canvas) {
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.DKGRAY);
canvas.drawRoundRect(new RectF(getWidth() - 100, getHeight() - 70,
getWidth() - 20, getHeight() - 20), 10, 10, paint);
paint.setColor(Color.LTGRAY);
paint.setTextSize(27);
paint.setTextSkewX((float) 0.2);
canvas.drawText("Save", getWidth() - 92, getHeight() - 35, paint);
}
private void isResetRequested() {
if (TouchXD >= (getWidth() - 100) && TouchYD >= getHeight() - 70
&& TouchXD <= (getWidth() - 20) && TouchYD <= getHeight() - 20){
exp = true;
}
}
}
请帮助我的朋友,因为我不知道如何解决这个问题。
在此先感谢。
如果你想要代码中的任何东西,然后告诉我,我会提供它。
嗨! getDrawnMessage中的位图始终为空。我的意思是大小是正确的,但你不使用你的画布,你通过这种方法! – Dima 2014-12-04 16:16:06