Gallery item is not updated while file downloading

2019-08-28 17:24发布

问题:

I am downloading a video from my application but that video is not currently show in gallery, Now i just restart my device and then it shows in gallery item. I don't understand why, Is any video tag set with the file which will tell android to add this item in gallery video.

This is my code

FileOutputStream fos = new FileOutputStream(filePath);

                long total = 0;
                int len = 0;

                byte[] buf = new byte[1024];

                while ((len = inputStream.read(buf)) > 0) {
                    if(!cancel)
                    {
                        fos.write(buf, 0, len);
                        // send update
                        total = total + len;


                    }
                    else
                    {
                        File f = new File(filePath);

                        System.out.println("Path is=========="+f.getAbsolutePath());

                        if(f.exists())
                            f.delete();

                        cancel = false;             

                        break;
                    }
                } 

please help, thanks in advance

回答1:

Ohk i found my own question's answer just add this below line whenever download will have been completed

sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File(filePath))));


回答2:

I try this and it worked..hope it help others too.

   MediaScannerConnection.scanFile(this, new String[] { f.getAbsolutePath()},
            null,
 new MediaScannerConnection.OnScanCompletedListener() {
    @Override
    public void onScanCompleted(String path, Uri uri) {

    }
    });