Android 将Uri存储到本地文件,并获取路径

用于解决不同手机获取相册路径问题、

public class CopyFileByUri {
    public static String getPath(Context context, Uri srcUri) {
        String path=context.getFilesDir()+"/file.png";//获取本地目录
        File file=new File(path);
        try {
            InputStream inputStream = context.getContentResolver().openInputStream(srcUri);//context的方法获取URI文件输入流
            if (inputStream == null) return "null";
            OutputStream outputStream = new FileOutputStream(file);
            copyStream(inputStream, outputStream);//调用下面的方法存储
            inputStream.close();
            outputStream.close();
            return path;//成功返回路径
        } catch (Exception e) {
            e.printStackTrace();
            return "null";//失败返回路径null
        }
    }

    private static void copyStream(InputStream input, OutputStream output){//文件存储
        final int BUFFER_SIZE = 1024 * 2;
        byte[] buffer = new byte[BUFFER_SIZE];
        BufferedInputStream in = new BufferedInputStream(input, BUFFER_SIZE);
        BufferedOutputStream out = new BufferedOutputStream(output, BUFFER_SIZE);
        int count = 0, n = 0;
        try {
            while ((n = in.read(buffer, 0, BUFFER_SIZE)) != -1) {
                out.write(buffer, 0, n);
                count += n;
            }
            out.flush();
            out.close();
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

借鉴

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
获取U盘文件路径的代码如下: ```java // 获取U盘存储设备的根目录 File usbDrive = new File("/mnt/usb_storage/USB_DISK1"); // 列出U盘根目录下所有文件 File[] files = usbDrive.listFiles(); // 遍历文件列表,获取文件路径 for (File file : files) { String filePath = file.getAbsolutePath(); // 处理文件路径 } ``` 获取本地文件路径的代码如下: ```java // 获取本地存储目录 File localDrive = Environment.getExternalStorageDirectory(); // 列出本地存储目录下所有文件 File[] files = localDrive.listFiles(); // 遍历文件列表,获取文件路径 for (File file : files) { String filePath = file.getAbsolutePath(); // 处理文件路径 } ``` 需要注意的是,在 Android 11 中,对于本地存储目录的访问需要使用 `MediaStore` API,而不是直接访问文件系统。例如,获取 Downloads 目录下所有文件的代码如下: ```java // 查询下载文件 Uri uri = MediaStore.Downloads.EXTERNAL_CONTENT_URI; String[] projection = {MediaStore.Downloads._ID, MediaStore.Downloads.DISPLAY_NAME}; String selection = null; String[] selectionArgs = null; String sortOrder = null; Cursor cursor = getContentResolver().query(uri, projection, selection, selectionArgs, sortOrder); // 遍历查询结果,获取文件路径 while (cursor.moveToNext()) { long id = cursor.getLong(cursor.getColumnIndex(MediaStore.Downloads._ID)); String fileName = cursor.getString(cursor.getColumnIndex(MediaStore.Downloads.DISPLAY_NAME)); Uri contentUri = ContentUris.withAppendedId(MediaStore.Downloads.EXTERNAL_CONTENT_URI, id); String filePath = getFilePathFromUri(contentUri); // 处理文件路径 } cursor.close(); // 将 content:// 形式的 Uri 转换为文件路径 private String getFilePathFromUri(Uri uri) { String[] projection = {MediaStore.Images.Media.DATA}; Cursor cursor = getContentResolver().query(uri, projection, null, null, null); if (cursor != null && cursor.moveToFirst()) { String path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA)); cursor.close(); return path; } return null; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值