这是我的活动代码。
public class MainActivity extends AppCompatActivity {
ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv=findViewById(R.id.chckExternal);
filter();
}
这是我的过滤文件代码...
//--------------------Filter files----------------
public ArrayList<File> findSongs(File root) {
ArrayList<File> al = new ArrayList<>();
for(File singleFile : root.listFiles())
{
if(singleFile.isDirectory() && !singleFile.isHidden() ) {
al.addAll(findSongs(singleFile));
} else if(singleFile.getName().endsWith(".mp3") ||
singleFile.getName().endsWith(".MP3")) {
al.add(singleFile);
}
}
return al;
}
这是在列表视图中设置过滤器值的方法。
public void filter(){
ArrayList<File>
arrayList=findSongs(Environment.getExternalStorageDirectory());
ArrayList<String> convertedItemsList=new
ArrayList(arrayList.size());
for(int j=0;j < arrayList.size(); j++) {
convertedItemsList.add(arrayList.get(j)
.getParentFile().getName().toString());
}
ArrayAdapter arrayAdapter=new
ArrayAdapter(this,android.R.layout.simple_list_item_1,
convertedItemsList);
lv.setAdapter(arrayAdapter);
}
}
此代码能够从设备的内部存储读取文件,但无法从外部存储(例如OTG和可移动Sd卡)读取。...
答案 0 :(得分:2)
此方法将帮助您获取可移动sdcard的路径,但无法为您提供通过OTG连接的笔式驱动器的路径。...
//--------------- Method to retrieve connected storage--------------------------//
private static final Pattern DIR_SEPORATOR = Pattern.compile("/");
public static String[] getStorageDirectories()
{
// Final set of paths
final Set<String> rv = new HashSet<String>();
// Primary physical SD-CARD (not emulated)
final String rawExternalStorage = System.getenv("EXTERNAL_STORAGE");
// All Secondary SD-CARDs (all exclude primary) separated by ":"
final String rawSecondaryStoragesStr = System.getenv("SECONDARY_STORAGE");
// Primary emulated SD-CARD
final String rawEmulatedStorageTarget = System.getenv("EMULATED_STORAGE_TARGET");
if(TextUtils.isEmpty(rawEmulatedStorageTarget))
{
// Device has physical external storage; use plain paths.
if(TextUtils.isEmpty(rawExternalStorage))
{
// EXTERNAL_STORAGE undefined; falling back to default.
rv.add("/storage/sdcard0");
}
else
{
rv.add(rawExternalStorage);
}
}
else
{
// Device has emulated storage; external storage paths should have
// userId burned into them.
final String rawUserId;
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1)
{
rawUserId = "";
}
else
{
final String path =
Environment.getExternalStorageDirectory().getAbsolutePath();
final String[] folders = DIR_SEPORATOR.split(path);
final String lastFolder = folders[folders.length - 1];
boolean isDigit = false;
try
{
Integer.valueOf(lastFolder);
isDigit = true;
}
catch(NumberFormatException ignored)
{
}
rawUserId = isDigit ? lastFolder : "";
}
// /storage/emulated/0[1,2,...]
if(TextUtils.isEmpty(rawUserId))
{
rv.add(rawEmulatedStorageTarget);
}
else
{
rv.add(rawEmulatedStorageTarget + File.separator + rawUserId);
}
}
// Add all secondary storages
if(!TextUtils.isEmpty(rawSecondaryStoragesStr))
{
// All Secondary SD-CARDs splited into array
final String[] rawSecondaryStorages =
rawSecondaryStoragesStr.split(File.pathSeparator);
Collections.addAll(rv, rawSecondaryStorages);
}
return rv.toArray(new String[rv.size()]);
}
它为您提供存储阵列。...尝试在联想,华为,荣耀,vivo上进行此测试