gpt4 book ai didi

java - 如何通过按钮OnClick保存Canvas图像?

转载 作者:行者123 更新时间:2023-12-02 03:52:24 26 4
gpt4 key购买 nike

我正在尝试创建带有保存到图库选项的画图应用程序。我想知道为什么我的 OnClick 方法不起作用。当我输入:

fos = new FileOutputStream(getFileName());

它有红色下划线,我不知道为什么。绘制 View 是在相对布局中设置的。我搜索了整个互联网,但没有找到解决这个问题的方法。

public class MainActivity extends AppCompatActivity {

Button save;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

save = findViewById(R.id.save);

final RelativeLayout canva = (RelativeLayout) findViewById(R.id.r1);
MyDrawView myDrawView = new MyDrawView(this);
canva.addView(myDrawView);

save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
canva.setDrawingCacheEnabled(true);
Bitmap b = canva.getDrawingCache();

FileOutputStream fos = null;
try {
fos = new FileOutputStream(getFileName()); /// it's underlined in red ERROR
} catch (FileNotFoundException e) {
e.printStackTrace();
}

b.compress(Bitmap.CompressFormat.PNG, 95, fos);
}
});

}
}
public class MyDrawView extends View {

private Bitmap mBitmap;
private Canvas mCanvas;
private Path mPath;
private Paint mBitmapPaint;
private Paint mPaint;

public MyDrawView(Context c) {
super(c);

mPath = new Path();
mBitmapPaint = new Paint(Paint.DITHER_FLAG);

mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(0xFF000000);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(3);
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
}

@Override
protected void onDraw(Canvas canvas) {
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);

canvas.drawPath(mPath, mPaint);
}

private float mX, mY;
private static final float TOUCH_TOLERANCE = 4;

private void touch_start(float x, float y) {
mPath.reset();
mPath.moveTo(x, y);
mX = x;
mY = y;
}
private void touch_move(float x, float y) {
float dx = Math.abs(x - mX);
float dy = Math.abs(y - mY);
if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
mX = x;
mY = y;
}
}
private void touch_up() {
mPath.lineTo(mX, mY);
// commit the path to our offscreen
mCanvas.drawPath(mPath, mPaint);
// kill this so we don't double draw
mPath.reset();
}

@Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();

switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
touch_start(x, y);
invalidate();
break;
case MotionEvent.ACTION_MOVE:
touch_move(x, y);
invalidate();
break;
case MotionEvent.ACTION_UP:
touch_up();
invalidate();
break;
}
return true;
}

public void clear(){
mBitmap.eraseColor(Color.TRANSPARENT);
invalidate();
System.gc();
}}

最佳答案

检查您的 getFileName() 方法,是否传递字符串。您也可以关注下面的内容。

 File file =  new 
File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "MY_IMAGE.PNG");

FileOutputStream out = new FileOutputStream(file);
b.compress(Bitmap.CompressFormat.PNG, 95, out);
out.flush();
out.close();
MediaScannerConnection.scanFile(mContext, new String[] { file.toString() }, null,new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.i("ExternalStorage", "Scanned " + path + ":");
Log.i("ExternalStorage", "-> uri=" + uri);
}
});

您的图像将以MY_IMAGE.PNG名称保存到外部存储器的图片目录中

关于java - 如何通过按钮OnClick保存Canvas图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56772189/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com