【问题标题】:Get reference from a String variable at defined position从定义位置的字符串变量获取引用
【发布时间】:2018-12-01 05:49:24
【问题描述】:

我有一个名为 paths 的字符串变量,它具有AbsolutePath 指向目录中所有名为“micky”的文件。 paths 变量的值是-

sdcard/0/albums/micky1.jpg
sdcard/0/albums/micky2.jpg
sdcard/0/albums/micky3.jpg
sdcard/0/albums/micky4.jpg

我怎样才能只访问 micky3?我希望将 micky3 的路径存储在一个新变量中,例如 MyvalueMyvalue="sdcard/0/albums/micky3.jpg"

我尝试使用paths[2]; 访问paths,但它给出的错误是它不是一个显而易见的数组值。 Log.d(paths); 返回所有内容。 如何从字符串变量中唯一地访问一个值?或者有什么简单的方法可以做到吗?

逻辑(我正在尝试做什么)- 在目录(和子目录)中搜索名为“myfile”的文件。如果它(目录)包含“myfile”,则获取它为AbsolutePath 并将其存储在名为myfileone 的变量中。重复此操作,直到找到所有文件并将它们存储在名为 myfiletwomyfilethreemyfilefour 等的变量中。

这可以实现吗?

【问题讨论】:

  • 有一点需要注意。在通过Toast.makeText(MainActivity.this,lines[0],Toast.LENGTH_LONG).show(); 访问Toast 中的paths 时,我会一一收到Toast 消息,即sdcard/0/albums/micky1.jpg,然后是第二个Toast msg sdcard/ 0/albums/micky1.jpg 等等..而不是单个 Toast 中的所有路径。这表明 String 变量已经单独存储了这些值。我如何分别访问它们?

标签: java android file-io absolute-path


【解决方案1】:

您可以通过换行符split 字符串:String[] values = paths.split("\n")

这样您就可以使用values[2] 访问第三行,就像您之前尝试的那样。

【讨论】:

  • 也许使用System.getProperty("line.separator")而不是"\\n"
  • 试过用这个。 values[1] 和 vales[2] ,给出数组超出范围的错误。只有 values[0] 是可访问的,它返回所有路径。
  • 在问题中添加了更多细节,请看一下。
【解决方案2】:

如果你有文件夹路径,在本例中为“sdcard/0/albums”,你可以这样做:

ArrayList<String> picturePaths = new ArrayList<String>();

File pictureFolder = new File("sdcard/0/albums");
for(File f: pictureFolder.listFiles()){
    if(!f.isDirectory()){
        picturePaths.add(f.getAbsolutePath());
    }
}

这将获得图片文件夹中所有文件的绝对路径。

搜索目录(和子目录)的方法是使用递归搜索方法。

public File searchForFile(File folderFile, String fileName){
    if(!folderFile.isDirectory()){ //If the folder to search isn't actually a folder
        return null;
    }
    for(File f: folderFile.listFiles()){ //Go over each file in the folder
        if(f.getName().equals(fileName) && !f.isDirectory()){ //If the name matches and it's not a folder
            return f; //Then this is the correct file and we can return it
        } else if(f.isDirectory()){ //Otherwise, if it's a folder
            File subFile = searchForFile(f, fileName); //Then search it for the filename
            if(subFile != null){ //and if it's found
                return subFile; //then return it
            }
        }
    }
    return null; //If we reach the end of the method, then we haven't found it.
}

你可以这样称呼它:

File desiredImageFile = searchForFile(new File("the folder path"), "file name");

如果找不到,它将返回null。

【讨论】:

  • 错误,数组越界。这真的很奇怪。我试过 ArrayList picturePaths = new ArrayList();图片路径.add(fil.getAbsolutePath()); Log.d("newval", picturePaths.get(1));这就是错误 - 由:java.lang.IndexOutOfBoundsException:无效索引 1,大小为 1 在 java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255) 在 java.util.ArrayList.get(ArrayList.java:第308章)
  • 我在 OP 中添加了更多细节,请看一下。
  • @Mukesh 我添加了一种方法,可以让您在文件夹及其子文件夹中搜索具有指定文件名的文件,这对您有很大帮助。
猜你喜欢
  • 2023-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-30
  • 1970-01-01
  • 2019-01-31
  • 1970-01-01
  • 2022-10-21
相关资源
最近更新 更多