【问题标题】:How to Read Pen Drives connected through OTG?如何读取通过 OTG 连接的笔式驱动器?
【发布时间】:2019-01-03 12:25:37
【问题描述】:

这是我的活动代码。

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 卡等外部存储读取文件....

【问题讨论】:

  • 您需要使用StorageManager.getStorageVolumes() 列出所有可用驱动器,然后使用StorageVolume.createAccessIntent() 获取权限。之后,您必须对文档提供程序而不是 Files 进行操作。
  • IOW,您在 Android 4.4+ 上没有对 removable storage 的直接读写权限。
  • 好吧,没有直接的读取方式,但是有什么技巧可以从这些类型的存储中获取文件
  • @Pawel 你能不能给你的评论一些代码
  • 请任何人帮助我

标签: android android-contentresolver usb-drive


【解决方案1】:

此方法可以帮助您获取可移动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()]);
}

它为您提供了一系列存储....试试这个已经在 lenovo、huawei、honor、vivo 上测试过的东西

【讨论】:

    猜你喜欢
    • 2013-03-12
    • 1970-01-01
    • 2010-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-25
    • 2010-10-05
    相关资源
    最近更新 更多