可以使用以下代码示例获取媒体存储库中指定文件名的图像:
public Bitmap getImageByName(String imageName) {
Bitmap bitmap = null;
String[] projection = {MediaStore.Images.Media.DATA};
String selection = MediaStore.Images.Media.DISPLAY_NAME + "=?";
String[] selectionArgs = {imageName};
Cursor cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
projection, selection, selectionArgs, null);
if(cursor != null && cursor.moveToFirst()){
String filePath = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
cursor.close();
bitmap = BitmapFactory.decodeFile(filePath);
}
return bitmap;
}
上述代码中,将查询的投影设置为MediaStore.Images.Media.DATA
,这将返回图像的文件路径信息。然后,通过在选择中设置MediaStore.Images.Media.DISPLAY_NAME
和selectionArgs
等参数,可以筛选出指定文件名的图像。最后,从媒体存储中获取文件路径并使用BitmapFactory.decodeFile()
方法将其解码为位图。
上一篇:按照文件名称追加文件