java - 目录中存储的文件返回空?

标签 java android

我已下载图像文件并保存在目录中,但它返回空列表文件。文件被下载并存储在目录中。我尝试了很多解决方案仍然没有解决。我正在奥利奥设备上工作并检查了运行时权限。

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

 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/

相关文章:

java - 使用非静态公共(public)最终变量公开实例常量

java - 为什么 Spliterator 的 getExactSizeIfKnown() 不调用 hasCharacteristics()

java - 如何在 Java 中从 xsd 创建对象树?

android - 为什么我可以将 null 参数传递给 Kotlin 中需要非 null 的 fun ?

java.lang.IndexOutOfBoundsException : Invalid index 18, 大小为 18

java - 静态 java 字符串周围的奇怪引号字符(在 android 上?)

java - java中大文件读取不一致

java - 将动态生成的数据注入(inject) JSON

java - 上传的 APK 使用与之前的 APK 不同的证书进行签名

android - 如何使用 Android 应用程序编写文件?