[英]How to read a Subdirectory from External Storage with getExternalStorageDirectory()
I am developing an Android app that should read Files from the External Storage and display them in a Listview; 我正在开发一个Android应用,该应用应从外部存储读取文件并在Listview中显示它们; creating the subdirectory on the External Storage works fine.
在外部存储上创建子目录工作正常。 The problem is reading from the Subdirectory of the External Storage and I still haven´t figured out how to solve this problem.
问题是从外部存储的子目录中读取的,但我仍然没有弄清楚如何解决此问题。 The Listview displays all the subdirectories of the External Storage and I just want to display the content in one particular subdirectory.
Listview显示了外部存储的所有子目录,我只想在一个特定的子目录中显示内容。 The problem consists in "casting" a String into a File Object, which is obviously not possible.
问题在于将字符串“广播”到文件对象中,这显然是不可能的。 Do I really need to open a FileInputStream for that?
我真的需要为此打开FileInputStream吗? Any help or hints would be very much appreciated.
任何帮助或提示将不胜感激。
I tried to attach the subdirectory as a string to the Environment.getExternalStorageDirectory() method, lik this: Environment.getExternalStorageDirectory() + "/Audio"; 我试图将子目录作为字符串附加到Environment.getExternalStorageDirectory()方法,就像这样:Environment.getExternalStorageDirectory()+“ / Audio”;
mTextMessage = findViewById(R.id.message);
BottomNavigationView navigation = findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener
(mOnNavigationItemSelectedListener);
mListView = (ListView) findViewById(R.id.listView1);
file = Environment.getExternalStorageDirectory() + "/Audio";
fileNameList = getFileListfromSDCard();
mAdapter = new FlAdapter(this, R.layout.fl_list_item, fileNameList);
mListView.setAdapter(mAdapter);
The error message: 错误信息:
error: incompatible types: String cannot be converted to File 错误:类型不兼容:字符串无法转换为文件
Because getExternalStorageDirectory() returns String , you need to convert it to file before assigning it to file. 由于getExternalStorageDirectory()返回String ,因此需要在将其分配给文件之前将其转换为文件。 You can try like this.
您可以这样尝试。
File file = new File(Environment.getExternalStorageDirectory()+"/Audio");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.