Android App → Problem mit dem Speichern von einem Bitmap–Objekt.

Mark79

Neues Mitglied
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...

Java:
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();
    }


}
 

Mark79

Neues Mitglied
Hallo,
ich hab das Problem mittlerweile gelöst. Es lag in der Methode: readCanvasFromFile()
Man musste BitmapFactory.decodeFile mit der Option inMutable=true; öffnen.
Zudem mCanvas = new Canvas(mBitmap2); aufrufen.

Java:
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();
    }
 
Ähnliche Java Themen
  Titel Forum Antworten Datum
W Prüfen, ob App auf Gerät installiert ist Problem S10 Android & Cross-Platform Mobile Apps 11
W In App Purchase Problem? Android & Cross-Platform Mobile Apps 36
W Problem mit Android Studio Android & Cross-Platform Mobile Apps 0
T Android R.string.test+i Problem Android & Cross-Platform Mobile Apps 2
K Android to Pi | Websocket Problem Android & Cross-Platform Mobile Apps 3
N Intent und finish() Problem Android & Cross-Platform Mobile Apps 5
B Android App Programmierung Einsteiger Problem Android & Cross-Platform Mobile Apps 4
emeraldo Android Problem mit Bottomnavmenu Android & Cross-Platform Mobile Apps 10
I Das Problem mit der Tastatur... android:windowSoftInputMode="adjustPan" Android & Cross-Platform Mobile Apps 1
A Android Android Studio Emulator Problem Android & Cross-Platform Mobile Apps 1
S Android Studio Bluetooth App Problem Android & Cross-Platform Mobile Apps 6
J TicTacToe Problem bei kontrolle Android & Cross-Platform Mobile Apps 7
J Button array ID Problem Android & Cross-Platform Mobile Apps 2
M Problem bei Werteübergabe, MSQL verbindung Android & Cross-Platform Mobile Apps 3
S Android Problem mit Android Virtual Device erstellung. Android & Cross-Platform Mobile Apps 2
Anfänger2011 Text to Speech Problem Android & Cross-Platform Mobile Apps 1
S Android Android java onclick listener Problem Android & Cross-Platform Mobile Apps 9
A Android Problem mit ListView und OnItemClickListener.. Android & Cross-Platform Mobile Apps 10
K Problem mit arraylist und button Android & Cross-Platform Mobile Apps 16
R W-Lan Problem über Sockets Android & Cross-Platform Mobile Apps 1
P ViewPager Problem Android & Cross-Platform Mobile Apps 1
A Android Problem mit Video von Youtube abspielen Android & Cross-Platform Mobile Apps 4
A Android Problem mit Zurücktaste und ausgabe der Aktuellen Seite Android & Cross-Platform Mobile Apps 6
B Android Problem mit Soundwiedergabe Android & Cross-Platform Mobile Apps 2
T Android Android Sensor: Java Problem Android & Cross-Platform Mobile Apps 1
G Problem beim Rendern von 3D-Objekt Android & Cross-Platform Mobile Apps 0
L Android Gyroscope Sensor Problem Android & Cross-Platform Mobile Apps 2
S Android GPS Problem Android & Cross-Platform Mobile Apps 24
J Eclipse Emulator Problem Android & Cross-Platform Mobile Apps 1
J Eclipse Emulator Problem Android & Cross-Platform Mobile Apps 0
B Android Problem mit Rückgabewert Android & Cross-Platform Mobile Apps 13
L Android komisches Bitmap-Größe-Problem Android & Cross-Platform Mobile Apps 8
D Android Layout Problem Android & Cross-Platform Mobile Apps 2
R Problem mit View in ScrollView Android & Cross-Platform Mobile Apps 6
R Eclipse + AndroidSDK - Problem mit Referenzen Android & Cross-Platform Mobile Apps 6
M Problem mit setOnClickListener Android & Cross-Platform Mobile Apps 4
DaniSahne96 Problem beim Appdebuggen auf Smartphone Android & Cross-Platform Mobile Apps 3
P Android Problem beim Widget - Denkfehler ? Android & Cross-Platform Mobile Apps 2
M GCM IntentService Problem Android & Cross-Platform Mobile Apps 3
D Android Gallery Problem Android & Cross-Platform Mobile Apps 5
P Problem mit Cell id Android & Cross-Platform Mobile Apps 6
L Android Problem mit "spinner" Android & Cross-Platform Mobile Apps 10
D Android problem mit geschwindigkeitsberechnung app Android & Cross-Platform Mobile Apps 2
E Android Problem mit Contact Provider Android & Cross-Platform Mobile Apps 1
H Android Problem mit ListActivity Android & Cross-Platform Mobile Apps 3
S Android Layout Problem mit fill_parent Android & Cross-Platform Mobile Apps 5
F Android ExpandableList, SimpleCursorTreeAdapter, Cursor Problem Android & Cross-Platform Mobile Apps 2
A Android Problem mit Long.getLong() bzw. Integer.getInteger() Android & Cross-Platform Mobile Apps 2
A Problem mit HTTP- Verbindung Android & Cross-Platform Mobile Apps 4
V [Java] und [JavaME] ClientServer StreamConnection . Problem beim lesen / schreiben Android & Cross-Platform Mobile Apps 2
F Eclipse JAD File erzeugen -- Problem Android & Cross-Platform Mobile Apps 10
R Ein Problem beim ausführen von folgendem Quelltext Android & Cross-Platform Mobile Apps 11
M Problem mit dem Auslesen von System Properties Android & Cross-Platform Mobile Apps 7
P wtk problem Android & Cross-Platform Mobile Apps 3
G Math exp() Problem Android & Cross-Platform Mobile Apps 4
G S40 Problem Android & Cross-Platform Mobile Apps 8
A Problem beim Subtrahieren eines Double von einem Double Android & Cross-Platform Mobile Apps 5
C Problem Device/Emulator wird nicht erkannt Android & Cross-Platform Mobile Apps 3
S Image Problem Android & Cross-Platform Mobile Apps 11
M Problem mit den Softkeys Android & Cross-Platform Mobile Apps 4
G J2ME jar-problem Android & Cross-Platform Mobile Apps 10
S Komisches Problem Android & Cross-Platform Mobile Apps 3
F Problem beim Erstellen der Jar File Android & Cross-Platform Mobile Apps 4
A Problem: Canvas-Grösse Motorola RAZR v3r Android & Cross-Platform Mobile Apps 8
S Problem mit Einbindung einer externer Bibliothek Android & Cross-Platform Mobile Apps 2
G Random - Problem Android & Cross-Platform Mobile Apps 5
E problem mit den resourcen Android & Cross-Platform Mobile Apps 2
O Problem mit Datagramconnection Android & Cross-Platform Mobile Apps 2
P Problem mit der Uhrzeit Android & Cross-Platform Mobile Apps 2
S Problem auf dem Handy Android & Cross-Platform Mobile Apps 3
W Foto machen und es direkt per URI speichern und auswählen Android & Cross-Platform Mobile Apps 4
M Firebase RealtimeDatabase - Daten aus Queries in CSV File speichern Android & Cross-Platform Mobile Apps 1
I Android CameraApp, eingebaute App nutzen und Ergebniss als Datei speichern Android & Cross-Platform Mobile Apps 0
R Android Daten speichern und anzeigen Android & Cross-Platform Mobile Apps 7
T Screenshot speichern ohne alte Datei überschreiben Android & Cross-Platform Mobile Apps 3
P Daten bzw Objekte auf das Handy lokal speichern Android & Cross-Platform Mobile Apps 29
P Objekt mit Shared preferences speichern Android & Cross-Platform Mobile Apps 0
J Download und speichern mit jsch klappt nicht Android & Cross-Platform Mobile Apps 5
L Android SQLite Datenbank auf SDCard speichern Android & Cross-Platform Mobile Apps 5
V Kennt jemand ein Programm , um .DEX Dateien zu öffnen, bearbeiten und wieder speichern? Android & Cross-Platform Mobile Apps 2
B Dateien Speichern Android & Cross-Platform Mobile Apps 6
G Android Daten in einer Datenbank speichern Android & Cross-Platform Mobile Apps 1
V Bytes (Wertebereich 0-255) über BLE empfangen,lesen und speichern Android & Cross-Platform Mobile Apps 1
JavaWolf165 Android Fehler beim Speichern/Downloaden einer Datei Android & Cross-Platform Mobile Apps 2
Anfänger2011 Speichern von bereits gefüllten Dateien Android & Cross-Platform Mobile Apps 8
M Android Speichern einer .txt Datei im InternalStorage Android & Cross-Platform Mobile Apps 2
B Fragen zum Speichern einer Datei auf dem Gerät? Android & Cross-Platform Mobile Apps 7
G Speichern jede zehnte sekunde Android & Cross-Platform Mobile Apps 1
G Sensordaten speichern Android & Cross-Platform Mobile Apps 3
S Dynamische EditText View eingaben in Datenbank speichern Android & Cross-Platform Mobile Apps 0
L Android Apache POI: Datei speichern geht nicht Android & Cross-Platform Mobile Apps 1
A Android Daten in der Cloud speichern (Google) Android & Cross-Platform Mobile Apps 1
Streeber Android ExpandableListView Child-Auswahl speichern Android & Cross-Platform Mobile Apps 3
S Android Geschossenes Foto in anderem Ordner speichern Android & Cross-Platform Mobile Apps 11
G Wissen frage - wo am besten Speichern. Android & Cross-Platform Mobile Apps 1
S Android Speichern von Informationen in Code oder extern Android & Cross-Platform Mobile Apps 0
E Android Zeichnen auf Canvas - Teilbereich als Bild speichern Android & Cross-Platform Mobile Apps 3
L Android Text aus Textfeld speichern Android & Cross-Platform Mobile Apps 5
R Android Objekt in bestimmten Pfad auf Android-gerät speichern Android & Cross-Platform Mobile Apps 8
G Datei im Ordner speichern Android & Cross-Platform Mobile Apps 2

Ähnliche Java Themen

Neue Themen


Oben