目录中存储的文件是否返回空?

时间:2019-04-30 08:09:36

标签: java android

我已经下载了图像文件并保存在目录中,但是返回空列表文件。文件被下载并存储在目录中。我尝试了许多解决方案,但仍未解决。我正在oreo设备上工作,并检查了运行时权限。

下面一行用于检查目录中的文件

 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);

    }

1 个答案:

答案 0 :(得分:0)

尝试更改:

File giftFolder = new File(getApplicationContext().getFilesDir() + "/SamplePhoto/");

收件人

File giftFolder = new File(getApplicationContext().getFilesDir() + "/SamplePhoto");

在其他任何地方也都使用"/SamplePhoto/"。访问目录时,不需要尾部斜杠。