【问题标题】:Get character from string between similar sings从相似歌曲之间的字符串中获取字符
【发布时间】:2014-05-22 09:18:24
【问题描述】:

我正在尝试在我的 android 设备中获取图像的路径,例如:

/storage/emulated/0/DCIM/Camera/NAME.jpg

只是试图获取图像名称,但我可以。

我正在尝试...

String s = imagePath; 

路径imagePath在哪里

            

s = s.substring (s.indexOf ("/") + 1); 
s.substring s = (0, s.indexOf () "."); 
Log.e ("image name", s);

它返回我:

storage/emulated/0/DCIM/Camera/NAME.jpg

我只想要

名称.jpg

【问题讨论】:

    标签: java android string substring


    【解决方案1】:

    你需要String.lastIndexOf():

    String imagePath = "/path/to/file/here/file.jpg";
    String path = imagePath.substring(imagePath.lastIndexOf('/') + 1);
    

    【讨论】:

    • 谢谢! 10 分钟后,我将接受的答案送给你!非常感谢!
    【解决方案2】:

    你可以这样做:

    File imgFile = new File(imagePath);
    String filename = imgFile.getFilename();
    

    当您想跨平台使用应用程序时,这会为您省去很多麻烦,因为在 Linux 上,“/”作为路径分隔符,在 Windows 上使用“\”

    【讨论】:

      【解决方案3】:

      如果你正在处理File对象,那么你可以使用它的预定义方法getName()

      即:

          File mFile = new File("path of file");
          String filename = mFile.getName();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-08-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多