Delete file from /data/data/com.example.../files/

2

Holaaa. I make a query. I have a program that saves images as follows:

     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
            }

This has worked very well for me so far, to save the images, and to show them in ImageView's. However, now I'm making a method to erase them, and I can not make it work ...

public boolean rmImage(String dir){
        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;
    }

The problem is that the path that is stored in the database is the following: /data/data/com.example.facu.dbproperty/files/imagenes/1-mkpb3smpf7h0v

And according to this How do I access the folder /data/data/my.package.com from android via code? I would need root access. So, how to correctly save the route, to be able to show it and then erase it? Or how to delete the file with the route that I already have?

Thanks hug!

    
asked by Facundo Curti 05.02.2017 в 19:25
source

1 answer

0

To use getFilesDir () you are certainly saving the images in storage internal.

File dir = new File(this.getFilesDir(), db.IMAGES_DIR);

However, in the method it does not specify receiving this path, it ensures that the value it receives is correct:

String pathArchivo = this.getFilesDir() + db.IMAGES_DIR + nombeArchivo;

and with this call your method:

rmImage(pathArchivo);

To delete a file inside the internal memory is to simply specify the name of the file (or extra directory) within getFilesDir() :

File dir = getFilesDir();
File file = new File(dir, "directorioextra/nombrearchivo");
boolean deleted = file.delete();
    
answered by 06.02.2017 в 19:23