android将路径转化为URI

本文介绍了如何将已获取的文件路径字符串转换为合法的URI格式,并提供了两种实现方式:使用Uri.parse方法和通过File对象创建URI。此外还分享了如何通过Environment.getExternalStorageDirectory()获取外部存储目录。
部署运行你感兴趣的模型镜像
如果已經取得了檔案的路徑的字串,那怎麼轉成URI呢?


String ImagePath = "file://" + 路徑;
Uri uri = Uri.parse(ImagePath);


重點在於要加上『file://』,才能成為合法的URI,不然該URI會無法表示相對應的檔案位置。

EX:file:///ola/android.txt

補充:
或是用Environment.getExternalStorageDirectory()取得sdcard位置。

TakePicFileName = "ola.jpg";
TakePicFilePath = Environment.getExternalStorageDirectory().toString() + "/DCIM/";
File tmpFile = new File(TakePicFilePath, TakePicFileName);
Uri uri = Uri.fromFile(tmpFile);

您可能感兴趣的与本文相关的镜像

Stable-Diffusion-3.5

Stable-Diffusion-3.5

图片生成
Stable-Diffusion

Stable Diffusion 3.5 (SD 3.5) 是由 Stability AI 推出的新一代文本到图像生成模型,相比 3.0 版本,它提升了图像质量、运行速度和硬件效率

Android 开发中,将音频文件的 URI 路径转换为字符串表示形式,可以通过 `Uri` 对象的 `getPath()` 或 `toString()` 方法实现。根据 URI 的类型(如 `file://` 或 `content://`),需要采取不同的处理方式以确保获取到正确的路径字符串。 ### URI 转换为字符串路径 对于 `file://` 类型的 URI,可以直接使用 `getPath()` 方法获取文件路径字符串: ```java Uri uri = Uri.fromFile(new File("/sdcard/audio.mp3")); String path = uri.getPath(); // 返回 "/sdcard/audio.mp3" ``` 对于 `content://` 类型的 URI(如通过系统文件选择器获取的 URI),则不能直接通过 `getPath()` 获取实际路径,而是需要通过 `ContentResolver` 查询: ```java private String getPathFromUri(Uri uri, Context context) { String path = null; Cursor cursor = context.getContentResolver().query(uri, null, null, null, null); if (cursor != null) { if (cursor.moveToFirst()) { int columnIndex = cursor.getColumnIndex(MediaStore.Audio.AudioColumns.DATA); if (columnIndex != -1) { path = cursor.getString(columnIndex); } } cursor.close(); } return path; } ``` 此方法适用于音频文件的 URI 转换,基于 `MediaStore.Audio.AudioColumns.DATA` 字段查询实际路径 [^3]。 ### 使用 Uri.decode() 处理编码路径 在某些情况下,URI 中的路径可能被 URL 编码,此时需要使用 `Uri.decode()` 方法进行解码: ```java String encodedPath = uri.getEncodedPath(); String decodedPath = Uri.decode(encodedPath); ``` 该方式适用于处理包含特殊字符的路径字符串 [^4]。 ### 使用 FileProvider 生成可共享的 URIAndroid 7.0 及以上版本中,若需将文件路径转换为 URI 以供其他应用访问,应使用 `FileProvider`: ```xml <provider android:name="androidx.core.content.FileProvider" android:authorities="${applicationId}.fileprovider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" /> </provider> ``` 在 `res/xml/file_paths.xml` 中定义访问路径: ```xml <?xml version="1.0" encoding="utf-8"?> <paths xmlns:android="http://schemas.android.com/apk/res/android"> <external-path name="external_files" path="." /> </paths> ``` 然后通过以下方式生成 URI: ```java File file = new File(context.getExternalFilesDir(null), "audio.mp3"); Uri uri = FileProvider.getUriForFile(context, context.getPackageName() + ".fileprovider", file); ``` 此方法确保了 URI 的安全性,并适配了 Android 7.0 及以上版本的文件访问限制 [^2]。 ### 总结 - 对于 `file://` 类型的 URI,使用 `getPath()` 可直接获取字符串路径。 - 对于 `content://` 类型的 URI,需通过 `ContentResolver` 查询获取实际路径。 - 若路径被编码,应使用 `Uri.decode()` 解码。 - 在 Android 7.0 及以上版本中,推荐使用 `FileProvider` 生成安全的 URI
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值