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