【问题标题】:File saved in downloads but does not appear in Download app文件保存在下载中但未出现在下载应用程序中
【发布时间】:2017-01-03 22:20:10
【问题描述】:

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

                });

}

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

    • 好吧,我已经尝试过了..它也不能正常工作...我已经使用了带有硬编码值的代码..现在我可以看到该文件,但无法打开它..我是对 mediascanner 到底是什么感到困惑? DownloadManager downloadManager = (DownloadManager)getSystemService(DOWNLOAD_SERVICE); downloadManager.addCompletedDownload("Image","Image Saved",true,"image.png",fileObj.getAbsolutePath(),1,true);
    猜你喜欢
    • 1970-01-01
    • 2014-07-11
    • 1970-01-01
    • 2018-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-24
    相关资源
    最近更新 更多