2014-03-27 19 views
0

如何顯示存儲到SD卡文件名的某些部分,我正在開發相機應用中,我正在拍攝的照片,並存儲到SD卡這樣的:如何顯示文件名的某些部分

AU_201403251160_4_7_B-4-7-001.jpg 

現在我展示所有圖像保存到列表,我需要只顯示下面的代碼作爲文件名,而不是AU_201403251160_4_7_B-4-7-001.jpg

 B-4-7-001.jpg 

代碼:

// ColImgName 
    TextView txtName = (TextView) convertView.findViewById(R.id.ColImgName); 
    strPath = ImageList.get(position).toString(); 

    // Get File Name 
    fileName = strPath.substring(strPath.lastIndexOf('/')+1, strPath.length()); 
    file = new File(strPath); 
+0

你要多少個字符保存,你怎麼知道它的B-4-7-001.jpg而不是7_B ... – Infested

+0

請讓它更清楚你需要保留的文件名的哪一部分。 – Savv

回答

0

這是非常簡單的我做了一個小的變化,見下圖:

fileName = strPath.substring(strPath.lastIndexOf('_')+1, strPath.length()); 
0
String[] a = "AU_201403251160_4_7_B-4-7-001.jpg".split("_"); 
    System.out.println(a[a.length - 1]); 

輸出:

B-4-7-001.jpg 

這是最小的,你顯然有一些檢查。

1
String data = "AU_201403251160_4_7_B-4-7-001.jpg"; 
    System.out.println(data.substring(data.indexOf("-") - 1, data.length())); 

輸出:

B-4-7-001.jpg 
+0

使用你的解決方案,我只得到這個:001.jpg – Sun

+0

@Moon以任何方式,這是不可能的..我張貼後檢查只..recheck它..IndexOf()返回的第一個位置的「 - 」thetaht介於B和4之間,並且來自我最後打印的字符。 –

0
String filename = "AU_201403251160_4_7_B-4-7-001.jpg"; 
System.out.println(filename.replaceFirst(".*_", "")); 

輸出:

B-4-7-001.jpg 
0
String data = "AU_201403251160_4_7_B-4-7-001.jpg"; 
System.out.println(data.substring(data.lastIndexOf("_")+1)); 

輸出:

B-4-7-001.jpg 
相關問題