0

I have a program that saves images in the app file directory, and his absolute path to a database:

 DBHelperPropiedades db = new DBHelperPropiedades(this);

    //Si retorna una imagen
    if (requestCode == PICK_IMAGE && resultCode == Activity.RESULT_OK) {
        if (data == null) { //Si esta vacio
            //Display an error
            return;
        }
        try { //Agrega la imagen

            //Crea la carpeta donde se guarda la imagen, si no existe
            InputStream isImg = this.getContentResolver().openInputStream(data.getData());
            //db.IMAGES_DIR es una cadena con valor "imagenes"
            File dir = new File(this.getFilesDir(),db.IMAGES_DIR);
            dir.mkdirs();

            //Coloca el nombre a la imagen (id+random)
            String imgName=idPropiedad+"-";
            imgName+=new BigInteger(20, new SecureRandom()).toString(32);

            //Obtiene el Output para la imagen
            File imgFile = new File(dir, imgName);
            FileOutputStream osImg = new FileOutputStream(imgFile);

            //Copia la imagen
            byte[] buf = new byte[1024];
            int len;
            while ((len = isImg.read(buf)) > 0) {
                osImg.write(buf, 0, len);
            }

            //Cierra los stream
            isImg.close();
            osImg.close();

            //Guarda la URL y la foreingKey en la base de datos
            String uriImg=imgFile.getAbsolutePath();
            if(!db.addImagen(idPropiedad, uriImg)){
                Toast.makeText(this, "Carga en Base de datos Fallida", Toast.LENGTH_SHORT).show();
            }

        } catch (IOException e) {
            //Error
        }

It works fine for showing the image into a ImageView. Anyway, now i'm trying to delete them. Here is a path example: /data/data/com.example.facu.dbproperty/files/imagenes/1-mkpb3smpf7h0v

File imgFile = new File(dir);
    if(imgFile.exists()) {
        if (imgFile.delete()) { //Se borra la imagen. Si se hizo bien, borra de la base de datos
            DBHelperPropiedades db = new DBHelperPropiedades(this);
            db.rmImage(dir);
        } else {//Si se hizo mal
            Toast.makeText(this, "Se ha producido un error al eliminar la imagen", Toast.LENGTH_SHORT).show();
        }
    }

    return true;
}

But imgFile.exists() is false, even when I can how it in a ImageView.

According to this: Deleting files from applications data folder and this https://es.stackoverflow.com/questions/37108/c%c3%b3mo-acedo-a-la-carpeta-data-data-my-package-com-desde-android-por-medio-de-c%c3%b3 (spanish), it is a permission problem... But I can't find a solution...

So... How can I fix this, and delete the file from my actual path (/data/data..), or how to save the images (in a safe directory, preventing from delete), and show/delete them as now...

Community
  • 1
  • 1
  • "Anyway, now i'm trying to delete them" -- so... what **exactly** is your problem? Your question does not appear to have any code for deleting this file, nor have you explained what specific problem you are encountering with that code. – CommonsWare Feb 05 '17 at 21:52
  • that should be an easy one.. please use a **private** mode while working with your files [android official storage docs](https://developer.android.com/guide/topics/data/data-storage.html#filesInternal)... and don't forget to add permissions as well :) – ymz Feb 05 '17 at 22:07
  • jaja sorryyy, I forgive to tell the problem. It says the file does not exist :P (Now i will edit the post). OK, thx ymz – Facundo Curti Feb 05 '17 at 22:16

0 Answers0