【问题标题】:How to get all files from external sdcard and phone storage (internal with mobile memory)?如何从外部 sdcard 和手机存储(内部带有移动内存)中获取所有文件?
【发布时间】:2014-02-08 09:40:46
【问题描述】:

我尝试从手机获取所有文件(外部 sdcard 和手机存储(内部带有移动内存))。

我尝试使用此代码Environment.getExternalStorageDirectory()。但从此我只能获取 sd 卡文件而不是手机存储。

请帮助我如何获取内存位置路径或文件。

【问题讨论】:

  • 您可以从文件系统的根目录/ 开始,但除非您以root 身份执行此操作,否则您将无权读取所有文件。

标签: android android-sdcard


【解决方案1】:
File file[] = Environment.getExternalStorageDirectory().listFiles();
    recursiveFileFind(file);

public void recursiveFileFind(File[] file1){
int i = 0;
String filePath="";
     if(file1!=null){
    while(i!=file1.length){
        filePath = file1[i].getAbsolutePath();
            if(file1[i].isDirectory()){
                    File file[] = file1[i].listFiles();
            recursiveFileFind(file);
            }
        i++;
        //Log.d(i+"", filePath);
    }
  }
}

如果这不起作用,那么在下面的代码中你可以提供外部路径和

获取所有文件。

private void getAllFilesOfDir(File directory) {
    Log.d(TAG, "Directory: " + directory.getAbsolutePath() + "\n");

    final File[] files = directory.listFiles();

    if ( files != null ) {
        for ( File file : files ) {
            if ( file != null ) {
                if ( file.isDirectory() ) {  // it is a folder...
                    getAllFilesOfDir(file);
                }
                else {  // it is a file...
                    Log.d(TAG, "File: " + file.getAbsolutePath() + "\n");
                }
            }
        }
    }
}

【讨论】:

  • 我知道这种方法,并且通过这种方法我已经获取了所有文件,但是当移动设备中存在 sdcard 时,然后 Environment.getExternalStorageDirectory() 提供 sdcard 绝对路径,但现在我也想从手机获取所有文件存储也。如何获取手机存储路径??
  • 文件 sdCardDirectory = Environment.getExternalStorageDirectory();
  • 文件路径 = Environment.getDataDirectory();
【解决方案2】:

此代码将提供内部文件。

    String[] SavedFiles;
    ArrayList<String> arrList;
    SavedFiles = getApplicationContext().fileList();


   arrList = new ArrayList<String>();
   if(SavedFiles != null)
          {
              for(int i=0; i<SavedFiles.length; i++)
              {
                  arrList.add(SavedFiles[i]);
              }


          }

【讨论】:

  • 它什么也没给
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-08-30
  • 2019-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多