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

时间:2016-08-27 10:10:53

标签: android android-sdcard android-file android-download-manager android-mediascanner

我正在下载保存图片,但是当我打开下载应用程序时,我看不到文件。我尝试使用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());
                    }

                });

}

1 个答案:

答案 0 :(得分:0)

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

Found2