从android中的SD卡中删除图像

时间:2016-11-10 13:16:33

标签: android android-imageview android-gridview

我尝试从SD卡,特定文件夹中选择图像并在我的gridView中成功显示它。我有一个问题。在gridview onClickListener我想删除这个图像的position.strange situation.file在我的文件夹中删除,但gridview仍然显示图像

    private void fetchGalleryImages() {
    Uri mImageUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
    final String[] columns = {MediaStore.Images.Media.DATA, MediaStore.Images.Media.DATE_ADDED};
    Cursor imagecursor = getContentResolver().query(mImageUri, columns, MediaStore.Images.Media.DATA + " like ? ", new String[]{"%/Download/New Folder%"}, null);
    galleryImageUrls = new ArrayList<>();
    for (int i = 0; i < imagecursor.getCount(); i++) {
        imagecursor.moveToPosition(i);
        int dataColumnIndex = imagecursor.getColumnIndex(MediaStore.Images.Media.DATA);//get column index
        galleryImageUrls.add(imagecursor.getString(dataColumnIndex));//get Image from column index
        Log.e("array path", galleryImageUrls.get(i));
    }
}
private void setUpGridView() {

    imagesAdapter = new GridView_Adapter(CustomGallery_Activity.this, galleryImageUrls, true);
    galleryImagesGridView.setAdapter(imagesAdapter);
    galleryImagesGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            File file = new File(galleryImageUrls.get(position));
            if (file.exists())
            {
                file.delete();
                imagesAdapter = new GridView_Adapter(CustomGallery_Activity.this, galleryImageUrls, true);
                galleryImagesGridView.setAdapter(imagesAdapter);
            }

        }
    });
}

抱歉,我误入歧途。我在OnClick中添加了这一行

 galleryImageUrls.remove(position);

我关闭了应用并再次运行,但我遇到了同样的问题..我想更新我的gridview

1 个答案:

答案 0 :(得分:-1)

这是正确答案。我解决了自己

 private void setUpGridView() {

    imagesAdapter = new GridView_Adapter(CustomGallery_Activity.this, galleryImageUrls, true);
    galleryImagesGridView.setAdapter(imagesAdapter);
    galleryImagesGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            File file = new File(galleryImageUrls.get(position));
            if (file.exists())
            {
                file.delete();
                deleteFileFromMediaManager(galleryImageUrls.get(position));
                galleryImageUrls.remove(position);
                imagesAdapter = new GridView_Adapter(CustomGallery_Activity.this, galleryImageUrls, true);
                galleryImagesGridView.setAdapter(imagesAdapter);
            }

        }
    });
}

private void deleteFileFromMediaManager(String path) {
    try {
        MediaScannerConnection.scanFile(CustomGallery_Activity.this, new String[] { path },
                null, new MediaScannerConnection.OnScanCompletedListener() {
                    public void onScanCompleted(String path, Uri uri) {
                        CustomGallery_Activity.this.getContentResolver()
                                .delete(uri, null, null);
                    }
                });
    } catch (Exception e) {
        e.printStackTrace();
    }

}