需求:

如题

状态:

minSdk 30 

通过new File[] 遍历/storage/enumlated/0/获取对应图片的File.

方法:

1.在res/xml下创建file_paths.xml文件


<?xml version="1.0" encoding="utf-8"?>

<paths>
    <external-path
        name="your_name"
        path="." />
</paths>

2.在AndroidManifest.xml注册provider

<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
// Uri权限
android:grantUriPermissions="true">
<meta-data
   android:name="android.support.FILE_PROVIDER_PATHS"
   android:resource="@xml/file_paths"/>
</provider>

3.代码获取图片Uri(如DCIM/Camera下的照片)

Uri uri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".fileprovider", imageFile);
Uri finalUri = uri.toString + "/" + imageFile.getName();
...
//设置缩略图
Glide.with(context)
.load()
.centerCrop(finalUri)
.into(imageThumbnail/*ImageView控件*/);
posted on 2023-04-17 17:32  我就像奶油  阅读(103)  评论(0)    收藏  举报