| 

.NET C# Java Javascript Exception

3
Hallo zusammen,

ich habe erst vor Kurzem mit Java- und Android-Programmierung angefangen. Hierbei handelt es sich nur um ein Übungsprojekt. Bei meinem Problem geht es um ein kleines Programm, bei dem man mit dem Finger auf das Display malen kann, was auch ohne Probleme funktioniert. Hinzu kommt jetzt eine Speichern- und Öffnen-Funktion. Das Programm speichert auch das gemalte als png-Datei <saveCanvasToFile()>. Jedoch nur solange, bis man einmal auf Öffnen <readCanvasFromFile()> geklickt hat. Danach übernimmt es die Änderungen nicht mehr und speichert nur das Bild nochmal, welches zuletzt vor dem ersten Öffnen gespeichert wurde. Es hat wohl was mit dem mBitmap2 Objekt zu tun, denke ich. Vielleicht kann mir einer von Euch helfen??


Hier ein Quellcode-Auszug:

class ZeichnenView extends android.support.v7.widget.AppCompatImageView{

private Canvas mCanvas;
private Path path1;
private Paint mBitmapPaint;
private Bitmap mBitmap2;

public ZeichnenView(Context context, AttributeSet attrs){
super(context, attrs);

mBitmapPaint = new Paint();
mBitmapPaint.setAntiAlias(true);
mBitmapPaint.setColor(Color.GREEN);
mBitmapPaint.setStyle(Paint.Style.STROKE);
mBitmapPaint.setStrokeJoin(Paint.Join.MITER);
mBitmapPaint.setStrokeWidth(12f);

path1 = new Path();


}

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


@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);

canvas.drawBitmap( mBitmap2, 0, 0, null);
mCanvas.drawBitmap( mBitmap2, 0, 0, null);
canvas.drawPath(path1, mBitmapPaint);
mCanvas.drawPath(path1, mBitmapPaint);


}


@Override
public boolean onTouchEvent(MotionEvent event) {

switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
path1.moveTo(event.getX(), event.getY());
break;
case MotionEvent.ACTION_MOVE:
path1.lineTo(event.getX(), event.getY());
invalidate();
break;
case MotionEvent.ACTION_UP:
invalidate();
break;
}
return true;
//return super.onTouchEvent(event);
}

public void saveCanvasToFile() throws FileNotFoundException {
File file = new File( getContext().getFilesDir().toString(), "test.png");

FileOutputStream out = null;
out = new FileOutputStream(file);
invalidate();

final boolean compress = mBitmap2.compress(Bitmap.CompressFormat.PNG, 90, out);


if(compress) Toast.makeText(getContext(), file.toString() + " wurde gespeichert.", Toast.LENGTH_LONG).show();

}



public void readCanvasFromFile(){
mCanvas.drawColor(Color.WHITE);

File file = new File( getContext().getFilesDir().toString(), "test.png");
mBitmap2 = BitmapFactory.decodeFile(file.toString());
invalidate();
path1.reset();
}


}
News:
03.01.2018
Mark79 11 2
2 Antworten
0
Ich denke, das Problem liegt in Methode readCanvasFromFile() ohne es ausprobiert zu haben.
public void readCanvasFromFile(){
//mCanvas.drawColor(Color.WHITE);

File file = new File( getContext().getFilesDir().toString(), "test.png");
mBitmap2 = BitmapFactory.decodeFile(file.toString());
//invalidate();
mCanvas = new Canvas(mBitmap2);
path1.reset();
}

Das gespeicherte Bild muss meiner Meinung nach der Zeichenfläche zugewiesen werden. Damit ist auch das Überschreiben des Hintergrunds nicht notwendig.

Vielleicht hilft der Hinweis ein wenig weiter...
04.01.2018
edvservice 1,4k 1 6
0
Hi,
danke für den Tipp. Das hatte ich auch mal ausprobiert, allerdings hast Du mich nochmal auf den richtigen Weg gebracht. Man muss dann die Datei zusätzlich noch mit der Option options.inMutable=true laden. Das hatte ich vorher nicht gemacht.

Vielen Dank dann nochmal !!!

Hier die Funktion, wie sie funktioniert...

public void readCanvasFromFile(){
mCanvas.drawColor(Color.WHITE);

try {
File file = new File(getContext().getFilesDir().toString(), "test.png");

BitmapFactory.Options options = new BitmapFactory.Options();
options.inMutable=true;
mBitmap2 = BitmapFactory.decodeFile(file.toString(),options);
mCanvas = new Canvas(mBitmap2);
}
catch(Exception ex)
{
Toast.makeText(getContext(), ex.toString(), Toast.LENGTH_LONG).show();
}

path1.reset();
invalidate();
}
04.01.2018
Mark79 11 2

Stelle deine Java-Frage jetzt!