AndroidQ:从文件中加载图片
创始人
2024-10-09 17:01:40
0

在Android Q及更高版本中,为确保应用程序具有适当的权限,需要使用不同的方法从文件加载图像。下面是使用ContentResolver和ImageDecoder加载图像的示例代码:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
    ContentResolver contentResolver = getContentResolver();
    Uri uri = Uri.parse("file:///storage/emulated/0/my_image.jpg");

    ImageDecoder.Source source = ImageDecoder.createSource(contentResolver, uri);
    try {
        Bitmap bitmap = ImageDecoder.decodeBitmap(source);
        imageView.setImageBitmap(bitmap);
    } catch (IOException e) {
        e.printStackTrace();
    }
} else {
    File file = new File(Environment.getExternalStorageDirectory(), "my_image.jpg");
    Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
    imageView.setImageBitmap(bitmap);
}

请注意,此代码将检查设备上的Android版本,如果版本为Android Q或更高版本,则使用ContentResolver和ImageDecoder加载图像。否则,它将使用传统的方法从文件加载图像。

相关内容

热门资讯

玩家必用!aapoker如何设... 玩家必用!aapoker如何设置胜率,wpk俱乐部辅助器,力荐教程(有挂神器);建议优先通过aapo...
科普攻略!epoker透视底牌... 科普攻略!epoker透视底牌,大菠萝789辅助器下载,大神讲解(有挂神器);致您一封信;亲爱大菠萝...
发现一款!wpk透视插件,we... 1、发现一款!wpk透视插件,wepoker轻量版透视系统,总结教程(有挂技巧);详细教程。2、we...
安装程序教程!wepoker有... 安装程序教程!wepoker有用吗,wepoker私人局辅助挂,教你教程(有挂方法),支持语音通讯、...
重大通报!wepoker有脚本... 重大通报!wepoker有脚本吗,wepoker私人局透视插件,2025新版(有挂攻略);玩家必备必...
一分钟了解!wepoker透视... 一分钟了解!wepoker透视脚本免费app,we-poker是什么软件,解密教程(有挂辅助)1、点...
关于!we-poker辅助软件... 《关于!we-poker辅助软件教程,智星德州插件,可靠教程(有挂辅助)》 we-poker辅助软件...
专业讨论!hhpoker开挂教... 专业讨论!hhpoker开挂教程,wpk透视插件,教你攻略(有挂透明)科技教程也叫必备教程,这是一款...
玩家必备科技!pokemmo脚... 1、玩家必备科技!pokemmo脚本辅助器下载,aapoker脚本怎么用,攻略方法(有挂攻略);详细...
分享实测!wepoker正确养... 分享实测!wepoker正确养号方法,wepoker有用吗,我来教教你(有挂辅助);1分钟了解详细教...