0

I am working on android application. I am getting the image from gallery. Also I am getting the image path from gallery. Now my requirement is I want to get only the image name with the extension . How can I do that? Please help me.

String imgpath =  "/mnt/sdcard/joke.png";

The image extension can be anything joke.png or joke.jpeg. I need to get the image name with extension finally.

i.e I want to split the above string and get only joke.png.

How can I achieve that? Please help me in this regard.

4

5 に答える 5

14
String imgpath = "/mnt/sdcard/joke.png";

String result = imgpath.substring(imgpath.lastIndexOf("/") + 1); 
System.out.println("Image name " + result);

出力:-

Image name joke.png

絶対ファイルパスを含む文字列からファイル名を取得するにはどうすればよいですか?を読む必要があります。

于 2013-09-13T13:15:17.640 に答える
4

他の Java プログラムと同じように、Android でこれを行うことができます。

String[] parts = imagepath.split("/");
String result = parts[parts.length-1];
于 2013-09-13T13:16:55.497 に答える
4
String s[] = imgpath.split("/");
String result = s[s.length-1];
于 2013-09-13T13:18:15.187 に答える
1
String imgName = imgpath.substring((imgpath.lastIndexOf("/") + 1), imgpath.length());
于 2013-09-13T13:32:46.127 に答える
0

それがあなたの手にあるハンマーであれば、正規表現でもこれを得ることができます:

String fileName = null;
Pattern pattern = Pattern.compile("(^|.*/)([^/]*)$");
Matcher m = pattern.getMatcher(filenameWithPath);
if(matcher.matches()) {

        fileName = matcher.group(2);
}

しかし、これをやろうとしないでください。これは読みにくく、おそらく他の方法よりも遅くなります。

于 2013-09-13T13:19:23.640 に答える