Gallery item is not updated while file downloading

2019-08-28 16:59发布

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

2条回答
看我几分像从前
2楼-- · 2019-08-28 17:25

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) {

    }
    }); 
查看更多
甜甜的少女心
3楼-- · 2019-08-28 17:35

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))));
查看更多
登录 后发表回答