如何使用MediaScannerConnection scanFile扫描视频?

时间:2018-11-21 19:34:35

标签: android android-studio

我的应用程序是从Internet下载图像/视频,我尝试扫描下载的图像/视频以显示在图库中,我确实扫描了下载的图像,但视频未显示在图库中。这是我的代码:

    @Override
    protected String doInBackground(String... strings) {
        file_name = strings[0].substring(strings[0].lastIndexOf("/") + 1);

        try {
            InputStream input = null;
            OutputStream output = null;
            HttpURLConnection connection = null;

            try {
                URL url = new URL(strings[0]);
                connection = (HttpURLConnection) url.openConnection();

                connection.connect();
                if (connection.getResponseCode() != HttpURLConnection.HTTP_OK)
                    return "Server returned HTTP " + connection.getResponseCode() + "  "
                            + connection.getResponseMessage();
                //}

                int filelength = connection.getContentLength();
                file_size = filelength;

                input = connection.getInputStream();
                output = new FileOutputStream(Environment.getExternalStorageDirectory().getAbsoluteFile()
                        + "/DownloadsFile/" + file_name);

                byte data[] = new byte[4096];
                long total = 0;
                int count;

                while ((count = input.read(data)) != -1) {
                    if (isCancelled()) {
                        return null;
                    }
                    total += count;
                    if (filelength > 0) {
                        publishProgress((int) (total * 100 / filelength));
                    }
                    output.write(data, 0, count);
                }
            } catch (Exception e) {
                return e.toString();
            } finally {
                try {
                    if (output != null) {
                        output.close();
                    }
                    if (input != null) {
                        input.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (connection != null) {
                    connection.disconnect();
                }
            }
        } finally{

        }

        return null;
    }

那么,如何使用MediaScannerConnection scanFile方法在图库中显示视频?

0 个答案:

没有答案