Search code examples
androidfilesd-cardfile-copying

Bug from delete image file using android


I have created a Customized Gallery app using Universal image loader methods

Now, am picking out an image from the Gallery and moving it to my app folder. And am deleting the particular selected image from the Gallery.The problem now is that even after the deletion of the image, the imge is still visible in the Gallery and blinks showing 0.0 KB memory in same file name.How to clear the Bug

Please help me how to solve this bug.

Note:

I have moved the image from the source path to the destination. And my destination received the particular image.

And then when i finally delete the image from the source part, the image is still visible same filename for example :IMG_20151106_193.jpg memory size 0.0kB

Code Line :

  @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    try {
        if (resultCode == RESULT_OK) {

            ArrayList<String> result = data.getStringArrayListExtra(Gallery_picker.KEY_ARRAYLIST_SELECTED_PHOTOS);
            mSelectedPhotos.clear();
            if (result == null) {
                return;
            }

            Iterator<String> iterator = result.iterator();
            while (iterator.hasNext())
            {
                String path = (String) iterator.next();
                if (!mSelectedPhotos.contains(path))
                {
                    mSelectedPhotos.add(path);
                }

            }

            String[] mStringArray = new String[mSelectedPhotos.size()];
            mStringArray = mSelectedPhotos.toArray(mStringArray);
            for(int i = 0; i <mStringArray.length ; i++)
            {

                String path=""+ (String)mStringArray[i];//it contain your path of image..im using a temp string..
                String filename=path.substring(path.lastIndexOf("/")+1);

                /*** Move file in SD Card ***/
                try {
                    File extStore = Environment.getExternalStorageDirectory();
                    fis = new File(path);
                    File fos = new File(extStore +"/Myfoldername/"+Subfoldername+"/"+customsimagename+".jpg");
                    copyFile(fis, fos); // Copy
                    fis.delete();

                } catch (Exception e)
                {

                    // TODO Auto-generated catch block
                    e.printStackTrace();
       //     Toast.makeText(this, "Failed! = " + e.getMessage(),Toast.LENGTH_LONG).show();
                }

            }
        }
    } catch (Exception e) {

    }


}        

Solution

  • After change imgae path,you should notify the gallery to update,so you should send a broadcast to make it. Before 4.4,you can call this:

    sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,Uri.parse("file://"+Environment.getExternalStorageDirectory())));  
    

    After 4.4,try this:

    sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + file)));//the file is new image's path