首页 > 其他分享 >Android 两种方获取U盘的挂载路径

Android 两种方获取U盘的挂载路径

时间:2023-11-29 17:33:05浏览次数:38  
标签:U盘 editPath lineList inputStream new 挂载 Android null String


第一种

public String getUsbPath() {
        try {
            StorageManager sm = (StorageManager) MyApplication.getContext().getSystemService(STORAGE_SERVICE);
            Method getVolumePathsMethod = StorageManager.class.getMethod("getVolumePaths",null);
            String[] paths = (String[]) getVolumePathsMethod.invoke(sm, null);
            return paths.length <= 1 ? null : paths[1];

        } catch (Exception e) {
            Log.e(TAG, "---getUsbPath() failed"+e);
        }
        return null;
    }

第二种

   private String getUsbPath() {
        String filePath = "/proc/mounts";
        File file = new File(filePath);
        List<String> lineList = new ArrayList<>();
        InputStream inputStream = null;
        try {
            inputStream = new FileInputStream(file);
            if (inputStream != null) {
                InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "GBK");
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                String line = "";
                while ((line = bufferedReader.readLine()) != null) {
                    if (line.contains("vfat")) {
                        lineList.add(line);
                    }
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        String path = null;
        if (lineList.size() > 0) {
            String editPath = lineList.get(lineList.size() - 1);
            int start = editPath.indexOf("/mnt");
            int end = editPath.indexOf(" vfat");
            path = editPath.substring(start, end);
        }
        return path;
    }
   

标签:U盘,editPath,lineList,inputStream,new,挂载,Android,null,String
From: https://www.cnblogs.com/wanglongjiang/p/17865434.html

相关文章

  • Android项目实战(六十七):自定义圆形进度条
    圆形进度条支持设置:1、圆环背景颜色2、圆管背景宽度3、进度圆环颜色4、进度圆环宽度5、圆环进度6、开始角度7、动画执行时间 自定义类:packagecom.example.mainactivty;importandroid.content.Context;importandroid.content.res.TypedArray;importandroid.......
  • OTG改为usb host接u盘鼠标外设使用
    OTG改为usbhost接u盘鼠标外设使用 1.在终端控制 查看USB0当前Role,如下图显示,查询结果是usb_hostcat/sys/devices/platform/soc/usbc0/otg_role手动切换到Host模式,如下图显示,host_chosefinished!cat/sys/devices/platform/soc/usbc0/usb_host手动切换到Device模式,......
  • Android开放配件 (AOA) 协议
    一、背景 自Android3.1之后的版本,Google引入了USBAccessories的概念,并提供了相关的开发库。Android3.1之后的版本不仅可以让Android设备作为USBHost的角色支持USB鼠标、键盘、游戏手柄等,还可以以USBDevice的角色与一些具有USBHost功能,但却扮演着配件角色的设备相连,Google......
  • Android之 看“马达”如何贯通Android系统 (从硬件设计 --> 驱动 --> HAL --> JNI -->
    Android之看“马达”如何贯通Android系统(从硬件设计-->驱动-->HAL-->JNI-->Framework-->Application)-如果天空不死-博客园https://www.cnblogs.com/skywang12345/p/3404808.html  在Android2.3(Gingerbread)系统的时候,我写过一篇关于“Android震动马达......
  • Android Compose 的分页(Paging3)
    Overview官方链接:https://developer.android.com/topic/libraries/architecture/paging/v3-overview需要注意的是,Paging库的组件在应用程序的三层中运行,Paging在三层的架构如下图:存储库层ViewModel层用户界面层在三层的数据传递如下图:方法/接口官方链接:https://dev......
  • Realtek蓝牙Android10.0移植结束后的基本测试和常见问题分析
    基本测试主要包括配置检查和BT测试两大部分配置检查:为了进一步确保porting没有问题,在测试之前先确认fw以及config文件是否存在。adbshell到测试平台的根目录,检查测试平台的vendor/firmware/目录中rtlxxxx_fw以及rtlxxxx_config文件是否存在(xxxx为BTChip型号)......
  • 直播系统代码,Android自定义View实现呼吸灯效果
    直播系统代码,Android自定义View实现呼吸灯效果自定义View的属性定义attrs.xml如下: <resources>  <declare-styleablename="BreathView">    <attrname="centerCircleRadius"format="dimension"/>    <attrname="circleCol......
  • 解决AndroidStudio 模拟器无网络连接
    解决AndroidStudio模拟器无网络连接主要原因是安卓模拟器的dns和电脑的dns不一致引起的,可以修改安卓模拟器的dns即可找到安卓模拟器的名字修改安卓模拟器dns命令 #Pixel7_API_30_fei这个是你自己模拟器的名字,也就是第一步中找的的模拟器名字./emulator-avdPixel......
  • android创建平板的分页页码
    在横向平板显示分页页码的时候,要实现下面的效果当默认分页超过5个之后中间显示...然后两边的页码按钮点击之后移动页码,点击1、2页码不移动,点击了第3页之后,左边移动到2、3、4页面,如下使用RecyclerView列表实现,通过对Item的type进行分类来实现页码按钮和省略号,下面是分页列表......
  • Android开发App回到桌面但不退出APP的实现
    方法1:Intentintent=newIntent();//创建Intent对象intent.setAction(Intent.ACTION_MAIN);//设置Intent动作intent.addCategory(Intent.CATEGORY_HOME);//设置Intent种类intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//标记context.startActivity(intent);方法2:......