Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/220.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何读取通过OTG连接的笔驱动器?_Android_Android Contentresolver_Usb Drive - Fatal编程技术网

Android 如何读取通过OTG连接的笔驱动器?

Android 如何读取通过OTG连接的笔驱动器?,android,android-contentresolver,usb-drive,Android,Android Contentresolver,Usb Drive,这是我的活动代码 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.c

这是我的活动代码

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;
}
/--------------筛选文件----------------
公共ArrayList findSongs(文件根目录){
ArrayList al=新的ArrayList();
对于(文件singleFile:root.listFiles())
{
if(singleFile.isDirectory()&&!singleFile.ishiden()){
al.addAll(findSongs(singleFile));
}else if(singleFile.getName().endsWith(“.mp3”)||
singleFile.getName().endsWith(“.MP3”)){
al.add(单文件);
}
}
返回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);
}
}
public void过滤器(){
ArrayList
arrayList=findSongs(Environment.getExternalStorageDirectory());
ArrayList convertedItemsList=新建
ArrayList(ArrayList.size());
对于(int j=0;j

此代码能够从设备的内部存储器读取文件,但无法从外部存储器(如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()]);
}
/--------------检索连接存储的方法--------------------------//
私有静态最终模式DIR\u SEPORATOR=Pattern.compile(“/”);
公共静态字符串[]GetStorageDirectory()
{
//最终路径集
最终集rv=新HashSet();
//主物理SD卡(非仿真)
最终字符串rawExternalStorage=System.getenv(“外部存储”);
//所有辅助SD卡(不包括主SD卡)之间用“:”分隔
最终字符串rawSecondaryStoragesStr=System.getenv(“SECONDARY_STORAGE”);
//主模拟SD卡
最后一个字符串rawsemulatedstoragetarget=System.getenv(“仿真存储目标”);
if(TextUtils.isEmpty(rawEmulatedStorageTarget))
{
//设备具有物理外部存储;使用普通路径。
if(TextUtils.isEmpty(rawExternalStorage))
{
//外部存储未定义;正在返回默认值。
rv.添加(“/storage/sdcard0”);
}
其他的
{
rv.添加(外部贮存);
}
}
其他的
{
//设备具有模拟存储;外部存储路径应具有
//用户ID被烧坏了。
最终字符串rawUserId;
if(Build.VERSION.SDK\u INT

它为您提供了一系列存储……请尝试这一已经在联想、华为、荣光、维梧(vivo)上测试过的功能,您需要使用它列出所有可用的驱动器,然后获得许可。之后,您必须在documents provider上操作,而不是在
文件上操作。注意,在Android 4.4+上,您没有直接的读写权限。好的,没有直接的读取方法,但是从这些类型的storage@Pawel你能为你的评论显示一些代码吗请任何人帮助我