我需要获取为用户选择的 CSV 文件的名称,但 getName()
方法没有返回任何值。
这是代码
private void readCSV(Uri uri) {
InputStream is;
File file = null;
try {
if (uri.getScheme().equals("file")) {
file = new File(uri.toString());
Log.i("File selected: ", file.getName()); //file.getName() doesn't work
is = new FileInputStream(file);
为什么不返回文件名?
编辑 1
private void readCSV(Uri uri) {
InputStream is;
File file;
try {
/*This conditional is false*/
if (uri.getScheme().equals("file")) {
file = new File(uri.toString());
Log.i("File selected: ", file.getName()); //file.getName() doesn't
is = new FileInputStream(file);
} else {
/* this part is the one that runs */
is = this.getContentResolver().openInputStream(uri);
Log.i("File selected: ", uri.getLastPathSegment()); //i tried this, it returns me 5049 but it is not the name of the selected file
}
最佳答案
uri.toString()
将返回对象引用而不是文件路径。
你应该调用 uri.getPath()
关于java - file.getName() 什么都不返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59475827/