【发布时间】: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