0

I have a group of images that users upload in my app. I store the image path in an sqlite database and the image in the internal storage of the app. I was able to go around deleting a single image if the user selects it and chooses to delete it. My problem now is that I have a clear all button sort of, that is meant to clear a particular group of images. How do I loop around this?

4

1 回答 1

0

在这里,我创建了一个 asynTask 类,用于一次删除多个文件。

private class DeleteFilesAsync extends AsyncTask<String, Integer, Integer> {

    ProgressDialog mProgressDialog;
    ArrayList<String> fileNames = new ArrayList<String>();

    public DeleteFilesAsync(ArrayList<String> fileNames) {
        this.fileNames = fileNames;
    }

    @Override
    protected void onPreExecute() {

        try {

            mProgressDialog = new ProgressDialog(
                    SavedImageListingActivity.this);
            mProgressDialog.setMessage("deleting...");
            mProgressDialog.show();

        } catch (Exception e) {
            // TODO: handle exception
        }

        super.onPreExecute();
    }

    @Override
    protected Integer doInBackground(String... params) {

        for (int i = 0; i < fileNames.size(); i++) {

            String fileName = fileNames.get(i);

            File file = new File(fileName);
            if (file.exists()) {
                if (file.isFile()) {
                    file.delete();
                    onProgressUpdate(i);
                }
            }

        }

        return null;
    }

    @Override
    protected void onPostExecute(Integer result) {

        try {

            mProgressDialog.dismiss();
        } catch (Exception e) {
            // TODO: handle exception
        }

        // Do more here after deleting the files

        super.onPostExecute(result);
    }

}

如何使用它

new DeleteFilesAsync(imgFilesSelected).execute("");

在哪里imgFilesSelected类型ArrayList<String> 在 imgFilesSelected 列表中添加所有文件路径,例如

imgFilesSelected.add("my_path_dir/filename.jpg");
imgFilesSelected.add("my_path_dir/filename2.png");
imgFilesSelected.add("my_path_dir/filename3.png"); // etc

然后将其传递给 DeleteFilesAsync() 类构造函数,如上所示。

全部完成。

于 2015-06-18T08:07:00.087 回答