How To Delete Image From My Custom Gallery And Then Refresh The Gallery
Actually I have made a Custom gallery. All the image are shown in my Custom gallery. But here is a problem. When delete the image using delete button, image files are deleted from
Solution 1:
before calling notifyDataSetChanged();
You should remove Deleted image from thumbnails
Bitmap[]
array. but if you can use List<Bitmap>
instead of that, the method will be like this :
publicvoidRemoveThumbnail(int position)
{
this.thumbnails.remove(position);
//notifyDataSetChanged() can be called in this method or after//calling this method in MainActivity
notifyDataSetChanged();
}
otherwise this is something that removes thumbnail in specific position :
publicvoidRemoveThumbnail(int position){
Bitmap[] temp = new Bitmap[thumbnails.length - 1];
int tempIndex = 0;
for (int i = 0 ; i < thumbnails.lenght ; i++)
{
if(i != position)
temp[tempIndex ++] = thumbnails[i];
}
thumbnails = temp;
//notifyDataSetChanged() can be called in this method or after//calling this method in MainActivitynotifyDataSetChanged();
}
Solution 2:
You should notify your adapter.
First declare your adapter (imageAdapter
for you) in top of codes (before onClickListeners) and then call this any time you change something (for example after deleting photos in your onClickListener):
imageAdapter.notifyDataSetChanged();
Solution 3:
Try sending a broadcast after delete a file. In Kotlin you can do :
privatefundeleteImage(path: String) {
val fDelete = File(path)
if (fDelete.exists()) {
if (fDelete.delete()) {
MediaScannerConnection.scanFile(this, arrayOf(Environment.getExternalStorageDirectory().toString()), null) { _, _ ->
Log.d("debug", "DELETE FILE DONE")
finish()
}
}
}
}
Post a Comment for "How To Delete Image From My Custom Gallery And Then Refresh The Gallery"