【问题标题】:How to create Directory and See it on Computer如何创建目录并在计算机上查看
【发布时间】:2014-03-25 12:25:39
【问题描述】:

我尝试在平板电脑上创建目录并想查看它。

我用这段代码创建目录

public void createDirectory(String sDirectoryName) {
  File direct = getDir(sDirectoryName, Context.MODE_PRIVATE);
  File fileWithinMyDir = new File(direct, "myfile");

  if(!direct.exist()) {
     System.out.println("Directory created");
  }
  else {
     System.out.println("Directory not created");
  }
}

我每次创建目录时都会看到,但是当我在文件系统中搜索文件夹时,我看不到它。我怎样才能让它可见。我做错了吗?

编辑:

我已授予清单上的所有权限。我也试过这段代码

File direct = new File(Environment.getExternalStorageDirectory()+"/"+sDirectoryName);

        if(!direct.exists())
        {
             if(direct.mkdir())
             {
                 System.out.println("Directory created");
                 Toast.makeText(MainActivity.this, "Directory created", Toast.LENGTH_LONG).show();
             }
             else
             {
                 System.out.println("Directory not created");
                 Toast.makeText(MainActivity.this, "Directory not created", Toast.LENGTH_LONG).show();
             }
        }

但这对我也不起作用。

编辑: 为了刷新,我使用此代码。

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));

工作。

【问题讨论】:

    标签: java android directory


    【解决方案1】:

    注意:由于 Android 使用 MTP 协议进行 USB 连接,有时文件或文件夹不会显示,因为所有内容都已缓存,可能需要刷新。

    更多信息:Nexus 4 not showing files via MTP

    【讨论】:

      【解决方案2】:

      如果文件不存在,则不会创建文件。它只是存储它的路径。您的 if 语句显示它不存在。

      【讨论】:

        【解决方案3】:

        试试这个……

        public void createDirectory(String sDirectoryName)
                    {    
            File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), sDirectoryName);
                                        if (!file.exists()) {
                                            file.mkdirs();
        
                                    }
            }
        

        【讨论】:

        • 我也想查看我电脑上的文件夹。也许它工作正常,但我希望看到它来保存文件。
        • 内部sd卡然后搜索你的文件夹
        • 我没有sd卡。我将它连接到计算机并尝试搜索。基本上我想在我的电脑上看到它
        • 在您的计算机上连接设备时,会在窗口资源管理器中创建便携式设备文件夹
        • 打开它并搜索您的文件夹
        【解决方案4】:

        使用以下代码创建目录。

        public void createDirectory(String sDirectoryName) {
          File direct = getDir(sDirectoryName, Context.MODE_PRIVATE);
          File fileWithinMyDir = new File(direct, "myfile");
        
          if(!direct.exist()) {
             direct.mkdirs();
             System.out.println("Directory created");
          }
          else {
             System.out.println("Directory not created");
          }
        }
        

        在 AndroidManifest.xml 中添加权限

        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
        

        【讨论】:

          【解决方案5】:

          确保你的manifeist具有以下权限

            <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
            <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
          

          在代码中

              File directory = new File(Environment.getExternalStorageDirectory()+DOC_FOLDER_NAME);
          
              // create directory if not exists 
              if(!directory.exists())
              {
                  if(directory.mkdirs()) //directory is created;
                      Log.i(" download ","App dir created");
                  else 
                      Log.w(" download ","Unable to create app dir!");
              }
          

          【讨论】:

            【解决方案6】:

            创建目录:

            if(!direct.exist()) {
               if (direct.mkdir())
                  Log.i(TAG, "Directory created");
               else
                  Log.w(TAG, "Failed to create directory");
            }
            

            并且不要忘记清单文件中的权限:

            <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
            

            【讨论】:

            • 检查.mkdir() 结果!
            【解决方案7】:

            您的打印语句令人困惑:

            if(!direct.exist()) { // If directory does not exist
                 System.out.println("Directory created"); // Directory created not true
            }
            

            由于只是创建一个File 对象,它不会创建代码应该是的目录:

            if(!direct.exist()) { // If directory does not exist
                 direct.mkdir(); // Create directory
                 System.out.println("Directory created");
            }
            else {
                 System.out.println("Directory not created");
            }
            

            还要确保在您的应用程序中添加android.permission.WRITE_EXTERNAL_STORAGE 权限。

            此外,它建议不要在 Android 中使用 System.out.println 作为在模拟器上,大多数设备 System.out.println 被重定向到 LogCat 并使用 Log.i() 打印。在非常旧的或自定义的 Android 版本上,这可能不是真的。

            【讨论】:

              【解决方案8】:

              在清单中添加:

              <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
              

              这个对于java文件:

              File myDirectory = new File(Environment.getExternalStorageDirectory(), "dirName");
              
              if(!myDirectory.exists()) {                                 
                myDirectory.mkdirs();
              }
              

              删除它:

               myDirectory.delete();
              

              这个是父目录的 File 对象:

               //create a File object for the parent directory
              
               File wallpaperDirectory = new File("/sdcard/Wallpaper/");
               // have the object build the directory structure, if needed.
               wallpaperDirectory.mkdirs();
               // create a File object for the output file
               File outputFile = new File(wallpaperDirectory, filename);
               // now attach the OutputStream to the file object, instead of a String representation
               FileOutputStream fos = new FileOutputStream(outputFile);
              

              【讨论】:

                【解决方案9】:

                问题不就行了 File direct = getDir(sDirectoryName, Context.MODE_PRIVATE);

                根据文档上下文。MODE_PRIVATE 将仅在应用程序本身内可见,其他程序或用户 ID 将无法找到它。

                尝试:

                File direct = getDir(sDirectoryName, Context.MODE_WORLD_READABLE);

                File direct = getDir(sDirectoryName, Context.MODE_WORLD_WRITEABLE);

                http://developer.android.com/reference/android/content/Context.html

                【讨论】:

                  猜你喜欢
                  • 2018-01-15
                  • 2011-08-22
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2012-07-10
                  相关资源
                  最近更新 更多