D
D
Destplay2017-01-30 05:28:27
Java
Destplay, 2017-01-30 05:28:27

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]);
    }

Then, the next time I load the image, I clean the database and delete the files, but the files are not deleted:
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);
    }

In the logs it says that the file is deleted, but in the gallery they only multiply (I upload every time).
PS: in the manifest:
<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

1 answer(s)
D
Destplay, 2017-01-30
@Destplay

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 question

Ask a Question

731 491 924 answers to any question