Answer the question
In order to leave comments, you need to log in
File.delete(); Why does not it work?
Good afternoon, I have such a problem, I can not delete a file from the gallery.
First, I save the link to the downloaded file in the database:
private void saveBase(int i){
ContentValues content = new ContentValues();
SQLiteDatabase database = pricedb.getWritableDatabase();
content.put("images", urs[i]);
long rowID = database.insert("db_prices", null, content);
Log.e(ConfigVar.LOG_TAG, "URI: "+ urs[i]);
}
private void deleteBase() {
SQLiteDatabase database = pricedb.getWritableDatabase();
Cursor cursor = database.query("db_prices", null, null, null, null, null, null);
if (cursor.moveToFirst()) {
int images = cursor.getColumnIndex("images");
do {
String image = cursor.getString(images);
File file = new File(image);
if (file.exists()) {
if (file.delete()) {
Log.e(ConfigVar.LOG_TAG, "Файл удалён: "+ image);
} else {
Log.e(ConfigVar.LOG_TAG,"Файл не найден :"+ image);
}
}
}
while (cursor.moveToNext());
}
database.delete("db_prices", null, null);
}
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Answer the question
In order to leave comments, you need to log in
The issue was resolved, despite the fact that in the logs he wrote "File deleted: /storage/emulated/0/Pictures/CafeHab/hamburger_big.png" in the gallery, -1 -2 was added to the file name, etc. example: "hamburger_big-1.png ". I had to make a cast)
urs[i] = urs[i].replaceAll(".png","-1.png");
urs[i] = urs[i].replaceAll(".jpg","-1.jpg");
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question