zoukankan      html  css  js  c++  java
  • Android File存储(一):文件的存储路径

    package com.gatsby.filepath;
    
    import android.os.Bundle;
    import android.os.Environment;
    import android.util.Log;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    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 MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            //内置Sdcard   /mnt/internal_sd
            String internalPath = Environment.getExternalStorageDirectory().toString();
            Log.d("gatsby", "path0->" + internalPath);
    
            //U盘挂载路径 /mnt/usb_storage/USB_DISK2/udisk0
            String usbPath = searchPath();
            Log.d("gatsby", "path2->" + usbPath);
    
        }
        
        public 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) {
                        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);
    
            return path;
        }
    
    }
  • 相关阅读:
    php流程控制
    php运算符
    php数据类型
    php基础
    谈谈2019年
    聊聊这三年
    第二次作业(源代码)
    个人介绍
    22.python匿名函数详解
    11.python内置模块之json模块
  • 原文地址:https://www.cnblogs.com/crushgirl/p/13131950.html
Copyright © 2011-2022 走看看