AliSherKashif / codenameone

Automatically exported from code.google.com/p/codenameone
0 stars 0 forks source link

FileSystemStorage.delete() don't delete captured files in android #411

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
The following code tries to
i) capture a photo
ii )materialize it using ImageIO
iii) then delete it using FileSystemStorage.delete()

- All seem to go well above API. It works perfectly in emulator.

How ever,

- when run in Samsung Galaxy SIII (android 4.1.1) the gallery app shows "black 
ghosts" for images captured. Those images cannot be opened, file manager 
doesn't show any related image files

- when run in Sony Xperia (android 4.0.4) the images are still visible in album 
app, both before and after booting the device (to check possible caching 
scenario)

All the best,
Jarkka Reunanen

------- (cut here) ------

package com.mycompany.myapp;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;

import com.codename1.capture.Capture;
import com.codename1.io.FileSystemStorage;
import com.codename1.ui.Button;
import com.codename1.ui.Form;
import com.codename1.ui.Image;
import com.codename1.ui.Label;
import com.codename1.ui.TextArea;
import com.codename1.ui.events.ActionEvent;
import com.codename1.ui.events.ActionListener;
import com.codename1.ui.layouts.BoxLayout;
import com.codename1.ui.util.ImageIO;

public class FileStorageTestForm extends Form implements ActionListener {

    private TextArea log;
    private Button b;
    private Button exit;

    public FileStorageTestForm() {
        this.getContentPane().setLayout(new BoxLayout(BoxLayout.Y_AXIS));
        this.b = new Button("Capture a photo");
        this.b.addActionListener(this);
        this.addComponent(this.b);

        this.exit = new Button("Exit");
        this.exit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Runtime.getRuntime().exit(0);
            }
        });
        this.addComponent(this.exit);

        this.log = new TextArea();
        log.setColumns(5);
        this.addComponent(this.log);
    }

    public void actionPerformed(ActionEvent evt) {

        // ----- test if photo capture is cancelled
        if (evt.getSource() == null) {
            return;
        }

        // ----- materialize and delete the picture
        if (evt.getSource() instanceof String) {
            try {
                String path = (String) evt.getSource();

                this.addLogMessage("Processing: " + path);
                FileSystemStorage fs = FileSystemStorage.getInstance();
                InputStream is = fs.openInputStream(path);

                ImageIO imageio = ImageIO.getImageIO();
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                imageio.save(is, out, ImageIO.FORMAT_JPEG, -1, 64, 1);
                Image im = Image.createImage(out.toByteArray(), 0,
                        out.toByteArray().length);
                is.close();

                Label l = new Label();
                l.setIcon(im);
                this.addComponent(l);

                fs.delete(path);
                if (fs.exists(path)) {
                    this.addLogMessage("... still there");
                } else {
                    this.addLogMessage("... deleted");
                }

            } catch (Throwable t) {
                this.addLogMessage("... !!! " + t.getClass() + ": " + t.getMessage());
            }

        }

        if (evt.getSource().equals(this.b)) {
            Capture.capturePhoto(this);
        }
    }

    private void addLogMessage(String mess) {
        this.log.setText(this.log.getText() + "\n" + mess);
    }

}

Original issue reported on code.google.com by jarkkare...@gmail.com on 5 Dec 2012 at 8:45

GoogleCodeExporter commented 9 years ago
This might be because the native device image gallery caches the thumbnail. I'm 
not sure we can avoid that but we'll look into that.

Original comment by shai.almog on 5 Dec 2012 at 12:20