Android 文件已保存在下载中,但未显示在下载应用中
我正在下载中保存图像,但当我打开下载应用程序时,我看不到该文件。我尝试使用MediaScannerConnection进行扫描,但仍然没有出现。这是我的代码。请帮忙Android 文件已保存在下载中,但未显示在下载应用中,android,android-sdcard,android-file,android-download-manager,android-mediascanner,Android,Android Sdcard,Android File,Android Download Manager,Android Mediascanner,我正在下载中保存图像,但当我打开下载应用程序时,我看不到该文件。我尝试使用MediaScannerConnection进行扫描,但仍然没有出现。这是我的代码。请帮忙 private void createPNGFile() { File downloadPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS); File createPNGI
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);
保存图像后,请尝试运行以下代码:
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri contentUri = Uri.fromFile(createPNGImage);
mediaScanIntent.setData(contentUri);
sendBroadcast(mediaScanIntent);
嗯,我试过了..它也不起作用..我使用了硬编码值的代码..现在我可以看到文件,但无法打开..我不知道mediascanner到底是关于什么的?下载管理器下载管理器=(下载管理器)getSystemService(下载服务);downloadManager.addCompletedDownload(“图像”,“图像已保存”,true,“Image.png”,fileObj.getAbsolutePath(),1,true);嗯,我试过了..它也不起作用..我使用了硬编码值的代码..现在我可以看到文件,但无法打开..我不知道mediascanner到底是关于什么的?下载管理器下载管理器=(下载管理器)getSystemService(下载服务);downloadManager.addCompletedDownload(“图像”,“图像已保存”,true,“Image.png”,fileObj.getAbsolutePath(),1,true);