Gallery item is not updated while file downloading

試著忘記壹切 提交于 2019-12-12 05:29:12

问题


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

    }
    }); 


来源:https://stackoverflow.com/questions/21934299/gallery-item-is-not-updated-while-file-downloading

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!