繁体   English   中英

文件已保存在下载中,但未显示在“下载”应用中

[英]File saved in downloads but does not appear in Download app

我正在下载保存图像,但是当我打开下载应用程序时,我看不到文件。 我尝试使用MediaScannerConnection进行扫描,但仍然没有出现。我的代码在哪里。 请帮忙

 private void createPNGFile()
    {
        File downloadPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
        File createPNGImage = new File(downloadPath.getAbsolutePath(),"image.png");

        try {
            InputStream is;
            is = getResources().openRawResource(R.raw.icon);
            OutputStream os = new FileOutputStream(createPNGImage);
            byte[] data = new byte[is.available()];
            is.read(data);

            os.write(data);
            is.close();
            os.close();
            Toast.makeText(this, "File Created (Path): " + createPNGImage.getAbsolutePath(), Toast.LENGTH_LONG).show();
            scanFiles(createPNGImage);
}catch(IOException e)
{
}

}

private void scanFiles(final File fileObj)
{
        MediaScannerConnection.scanFile(this, new String[]{

                        fileObj.getAbsolutePath()},

                null, new MediaScannerConnection.OnScanCompletedListener() {

                    public void onScanCompleted(String path, Uri uri)

                    {

                        Log.d("Media Scan", "Scan Completed" + fileObj.getAbsolutePath());
                    }

                });

}

保存图像后尝试运行以下代码:

Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri contentUri = Uri.fromFile(createPNGImage);
mediaScanIntent.setData(contentUri);
sendBroadcast(mediaScanIntent);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM