gpt4 book ai didi

android - 大小为 0kb 的位图图像保存

转载 作者:行者123 更新时间:2023-11-30 03:20:29 26 4
gpt4 key购买 nike

我尝试了几种代码将我的自定义 View 保存为 .jpg 格式。我正在将 View 转换为位图,然后将其保存,如下所示。但每次尝试时,保存的图像大小始终为 0 Kb,并且文件不包含任何内容。我已经获得了以下权限。

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

这是我用过的代码之一;

drawView.setDrawingCacheEnabled(true);
drawView.buildDrawingCache();
Bitmap b = drawView.getDrawingCache();
private void SaveImage(Bitmap finalBitmap) {

String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/saved_images");
myDir.mkdirs();
String fname = "Image.jpg";
File file = new File (myDir, fname);
if (file.exists ()) file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();

} catch (Exception e) {
e.printStackTrace();
}
}

我还想明确指出这段代码的主要思想不是我的。

整个完整代码; 公共(public)类 DrawPad 扩展 View {

//drawing path
private Path drawPath;
//drawing and canvas paint
private Paint drawPaint, canvasPaint;
//initial color
private int paintColor =0xff000000;
//canvas
private Canvas drawCanvas;
//canvas bitmap
private Bitmap canvasBitmap;

//erase flag
private boolean erase=false;

public DrawPad(Context context, AttributeSet attrs){
super(context, attrs);
setupDrawing();


}
public Paint getPaint(){
return drawPaint;
}
//setup drawing
private void setupDrawing(){

//prepare for drawing and setup paint stroke properties

drawPath = new Path();
drawPaint = new Paint();
drawPaint.setColor(paintColor);
drawPaint.setAntiAlias(true);
drawPaint.setStrokeWidth(1.0f);
drawPaint.setStyle(Paint.Style.STROKE);
drawPaint.setStrokeJoin(Paint.Join.ROUND);
drawPaint.setStrokeCap(Paint.Cap.ROUND);
canvasPaint = new Paint(Paint.DITHER_FLAG);

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

//draw the view - will be called after touch event
@Override
protected void onDraw(Canvas canvas) {
canvas.drawBitmap(canvasBitmap, 0, 0, canvasPaint);
canvas.drawPath(drawPath, drawPaint);
}

//register user touches as drawing action
@Override
public boolean onTouchEvent(MotionEvent event) {
float touchX = event.getX();
float touchY = event.getY();
//respond to down, move and up events
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
drawPath.moveTo(touchX, touchY);
break;
case MotionEvent.ACTION_MOVE:
drawPath.lineTo(touchX, touchY);
break;
case MotionEvent.ACTION_UP:
drawPath.lineTo(touchX, touchY);
drawCanvas.drawPath(drawPath, drawPaint);
drawPath.reset();
break;
default:
return false;
}
//redraw
invalidate();
return true;

}

//update color
public void setColor(String newColor){
invalidate();
paintColor = Color.parseColor(newColor);
drawPaint.setColor(paintColor);
}

//set erase true or false
public void setErase(boolean isErase){
erase=isErase;

if(erase){
drawPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
drawPaint.setStrokeWidth(15.0f);
}
else {
drawPaint.setStrokeWidth(1.0f);
drawPaint.setXfermode(null);
}
}

//start new drawing
public void startNew(){
drawCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
drawPaint.setStrokeWidth(1.0f);
invalidate();
}
public void SaveImage(Bitmap finalBitmap,String spin) {
if(finalBitmap == null) {
Toast unsavedToast = Toast.makeText(getContext(),
"Oops! Page could not be saved.", Toast.LENGTH_SHORT);
unsavedToast.show();
}

this.setDrawingCacheEnabled(true);
this.buildDrawingCache();
finalBitmap=this.getDrawingCache();
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/answers");
myDir.mkdirs();
String fname = spin+".jpg";
File file = new File (myDir, fname);
if (file.exists ()) file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
if ( finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out)==true){
out.flush();
out.close();
}
if(finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out)==false){
Toast fToast = Toast.makeText(getContext(),
"compression failed", Toast.LENGTH_SHORT);
fToast.show();
}

} catch (Exception e) {
e.printStackTrace();
}
}
}

最佳答案

检查finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);的返回值

documentation表示如果成功压缩到指定流,该方法将返回 true。我敢打赌它会为您返回 false。

检查您要压缩的位图中的内容。它似乎是空的,或者它有什么问题。找到创建位图的位置,并检查每一步是否成功。就像 finalBitmap.compress 在成功/失败时返回 true/false 一样,从创建位图开始检查位图上的每个操作,因为它看起来好像某处出了问题。

如果您仍然无法弄清楚,请在您的帖子中添加所有操作 finalBitmap 的代码,然后再进入您的 SaveImage 方法。 (顺便说一句,Java 中的方法应该以小写字母开头,例如 saveImage。)

关于android - 大小为 0kb 的位图图像保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19239656/

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