问题描述
Android从相册中选择照片时选完后显示不出来,打断点发现srcPath
图片路径正确,bitmap竟然为空😺‘。
bitmap = BitmapFactory.decodeFile(srcPath, opts);
检查一下👀:
- opts属性没问题
opts.inJustDecodeBounds = false;
- AndroidManifest中权限申请,没问题
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
- 动态权限申请
private static final String[] PERMISSION_EXTERNAL_STORAGE = new String[] {
Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE
};
private static final int REQUEST_EXTERNAL_STORAGE = 100;
......
private void verifyStoragePermissions(Activity activity) {
int permissionWrite = ActivityCompat.checkSelfPermission(activity,
Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permissionWrite != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(activity, PERMISSION_EXTERNAL_STORAGE,
REQUEST_EXTERNAL_STORAGE);
}
}
- 竟然还是不行🐶,我接着找🥱
最后看到一篇帖子讲需要在AndroidManifest.xml中添加android:requestLegacyExternalStorage="true"
原因如下(拷图过来,链接https://www.freesion.com/article/3094311913/):