-2

我已经实现了用于拆分文件路径并在文本视图中获取实际文件名的 android 应用程序。当我运行应用程序时,它会显示文件路径中的所有字符串名称。我只想要一个文件名(introduction.ppt)。如何在文本视图中获取此文件名?

这是我的代码:

String path = "/mnt/sdcard/coverted/introduction.ppt";    
String[] strAfterSpilt = path.split("/");

for(int j = 0; j < strAfterSpilt.length; j++) {
    if(strAfterSpilt[j].length() > 0) {
        String strFinalSplit = strAfterSpilt[j];
        System.out.println("strFinalSplit :-> " + strFinalSplit);
        function_arg1.setText(strFinalSplit);
    } else { break; } 
}

而且我必须尝试这种分割文件路径的方式;

String[] strAfterSpilt = path.split("/");

for (String str : strAfterSpilt) {
    System.out.println("str" + str.lastIndexOf(strAfterSpilt[i]));
}
4

4 回答 4

0
String path = "/mnt/sdcard/coverted/introduction.ppt";
String fileName = path.substring(path.lastIndexOf("/")+1);

会做你的工作。

于 2013-06-14T06:16:31.167 回答
0
String[] tokens = path.split("/");
String fileName = tokens[tokens.length - 1];
于 2013-06-14T06:18:54.893 回答
0

只需尝试以下代码..指定所需字符串的开始和结束位置。

String path = "/mnt/sdcard/coverted/introduction.ppt";
String fileName = path.substring(path.lastIndexOf("/"), path.length);
于 2013-06-14T06:28:47.633 回答
0

试试下面的代码

public class MainActivity extends Activity {
String path = "/mnt/sdcard/coverted/introduction.ppt";
String fileName;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
            // To get the File name
    fileName = path.substring(path.lastIndexOf("/")+1);
    TextView remotefilePath =(TextView)findViewById(R.id.txtFileName);
    remotefilePath.setText(fileName);
    }
}
于 2013-06-14T07:10:36.473 回答