Android File存储:文件的存储路径
Posted Gatsby
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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; } }
以上是关于Android File存储:文件的存储路径的主要内容,如果未能解决你的问题,请参考以下文章
如何获取原始文件夹uri路径并保存在android的内部存储器中?
Android开发笔记(一百八十五)使用设备浏览器Device File Explorer导出存储卡的文件