gpt4 book ai didi

android - 如何读取通过 OTG 连接的笔式驱动器?

转载 作者:搜寻专家 更新时间:2023-11-01 09:22:10 25 4
gpt4 key购买 nike

这是我的 Activity 代码。

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;
}

这是在 ListView 中设置过滤器值的方法。

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 卡)中读取....

最佳答案

此方法可以帮助您获取可移动SD卡的路径,但无法为您提供通过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 上测试过

关于android - 如何读取通过 OTG 连接的笔式驱动器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54022262/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com