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

相关内容

热门资讯

最终!we poker游戏下(... 最终!we poker游戏下(透视)聚友联盟免费辅助器(好像真的是有辅助app)-哔哩哔哩1、该软件...
备受关注的!wepokerpl... 备受关注的!wepokerplus开挂(透视)浙江宝宝游戏透视辅助器(原来是真的辅助工具)-哔哩哔哩...
事发当天!德州局怎么透视(透视... 事发当天!德州局怎么透视(透视)透视辅助器免费蜀山四川(竟然真的有辅助神器)-哔哩哔哩透视辅助器免费...
有玩家发现!wepoker私人... 有玩家发现!wepoker私人局俱乐部怎么进(透视)火神大厅辅助器(切实真的有辅助平台)-哔哩哔哩1...
透视黑科技!wepoker脚本... 透视黑科技!wepoker脚本下载(透视)花城棋舍插件安装(其实是真的辅助辅助器)-哔哩哔哩1、起透...
透视脚本!德普之星辅助器(透视... 透视脚本!德普之星辅助器(透视)乐逍遥脚本辅助(其实有辅助神器)-哔哩哔哩1、实时乐逍遥脚本辅助透视...
透视软件!wepoker安装教... 透视软件!wepoker安装教程(透视)福建开心辅助(果然真的是有辅助app)-哔哩哔哩1、实时福建...
透视辅助!wepoker钻石怎... 透视辅助!wepoker钻石怎么看底牌(透视)潮汕来物局透视平台使用方法(总是有辅助app)-哔哩哔...
据统计!wpk软件是正规的吗(... 据统计!wpk软件是正规的吗(透视)拱趴大菠萝调胜率(确实真的有辅助工具)-哔哩哔哩一、拱趴大菠萝调...
透视苹果版!德普之星透视免费(... 透视苹果版!德普之星透视免费(透视)微信中至有辅助吗(总是存在有辅助app)-哔哩哔哩1、用户打开应...