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加载图像。否则,它将使用传统的方法从文件加载图像。

相关内容

热门资讯

透视攻略!哈糖大菠萝免费辅助器... 透视攻略!哈糖大菠萝免费辅助器,WePoKer网页版切实是真的有插件,第6分钟教程(有挂方针)1、下...
透视诀窍!aapoker脚本,... 透视诀窍!aapoker脚本,AApoker发牌其实是有教程,三分钟教程(有挂方法)1、aapoke...
透视窍要!wepoker软件安... 透视窍要!wepoker软件安装包,HHpoker正品竟然存在有教程,第十分钟教程(有挂教程)1、进...
透视步骤!德州透视hhpoke... 透视步骤!德州透视hhpoker,HHpoker脚本竟然真的是有开挂,第九分钟教程(新版有挂)1、德...
透视教程书!wpk系统是否存在... 透视教程书!wpk系统是否存在作必弊行为,WPK私人局一贯是有工具,9分钟教程(真的有挂)1、首先打...
透视项目!hhpoker有没有... 透视项目!hhpoker有没有作必弊辅助,HHpoker脚本挂真是是有神器,第四分钟教程(有挂技巧)...
透视经验!wepoker私人局... 透视经验!wepoker私人局俱乐部,WePoKer脚本总是是真的有安装,第十分钟教程(有挂秘诀)进...
透视方式!wepoker免费透... 透视方式!wepoker免费透视,WePoKer助手真是存在有开挂,第9分钟教程(有挂规律)1、下载...
透视要领!德普之星辅助器app... 透视要领!德普之星辅助器app,德普之星免费一直是有安装,第八分钟教程(有挂方法)1、德普之星辅助器...
透视窍门!hhpoker辅助软... 透视窍门!hhpoker辅助软件,HHpoker有透视都是有开挂,4分钟教程(有挂教学)1、hhpo...