Android 读取U盘文件

一、通过adb 方式连接Android设备,然后通过adb 命令也就是Linux 命令查看Android设备相关的目录,

      下图就是我通过adb命令查看到我插入Android设备U盘的相关信息

《Android 读取U盘文件》

 

上面我们可以很清楚看到U盘挂载的路径是 /mnt/media_rw,而我的U盘名称是B4FE-5315,通过adb命令查看到自己想要的路径了,那么就是如何获取路径的问题了

二、获取U盘路径

本代码主要参考了:https://www.jianshu.com/p/b2425efca483

import android.util.Log;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class ReadUDiskUtil {

    public final static String searchPath() {
        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) {
//                    Log.e("TAG",line);
                    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 editPath = lineList.get(lineList.size() - 1);
        int start = editPath.indexOf("/mnt");
        int end = editPath.indexOf(" vfat");
        String path = editPath.substring(start, end);
        Log.d("TAG_SelectBusLineDialog", "path: " + path);
        return path;
    }
}

 

三、使用

比如使用VideoView播放视频

uri = Uri.parse(ReadUDiskUtil.searchPath()+"/video.mp4");
videoView.setVideoURI(uri);
videoView.start();

 

 

    原文作者:天空编程
    原文地址: https://blog.csdn.net/u011905195/article/details/113866361
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞