我已下载图像文件并保存在目录中,但它返回空列表文件。文件被下载并存储在目录中。我尝试了很多解决方案仍然没有解决。我正在奥利奥设备上工作并检查了运行时权限。
下面一行用于检查目录内的文件
try{
File giftFolder = new File(getApplicationContext().getFilesDir() + "/SamplePhoto/");
if (!giftFolder.exists()) {
giftFolder.mkdir();
}
File directory = new File(getApplicationContext().getFilesDir() + "/SamplePhoto/");
if(directory.isDirectory()){
Log.d("Files", "directory: "+ directory.isDirectory());
}
File[] files = directory.listFiles();
Log.d("Files", "Size: "+ files.length);
for (int i = 0; i < files.length; i++)
{
Log.d("Files", "FileName:" + files[i].getName());
}
}catch (Exception e){
e.printStackTrace();
}
以下代码用于使用下载管理器下载文件
public static void downloadFile(Context context, String url, String fileName) {
DownloadManager.Request downloadRequest = new DownloadManager.Request(Uri.parse(url));
downloadRequest.setTitle(fileName);
// in order for this if to run, you must use the android 3.2 to compile your app
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
downloadRequest.allowScanningByMediaScanner();
downloadRequest.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
File giftFolder = new File(context.getFilesDir() + "/SamplePhoto");
if (!giftFolder.exists()) {
giftFolder.mkdir();
}
// downloadRequest.setDestinationInExternalPublicDir(Environment.getExternalStorageState()+"/SamplePhoto/", fileName);
// downloadRequest.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName);
downloadRequest.setDestinationInExternalPublicDir(context.getFilesDir() + "/SamplePhoto/", fileName);
Log.e("DownloadingPath",""+ context.getFilesDir() + "/SamplePhoto/"+ fileName);
DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(downloadRequest);
}
最佳答案
尝试改变:
File giftFolder = new File(getApplicationContext().getFilesDir() + "/SamplePhoto/");
至
File giftFolder = new File(getApplicationContext().getFilesDir() + "/SamplePhoto");
在其他任何地方您也可以使用"/SamplePhoto/"
。访问目录时不需要尾部斜杠。
关于java - 目录中存储的文件返回空?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55916499/