Android 将本地图片转为Drawable对象

    /**
     * 将本地图片转换为 Drawable
     * @param context 上下文
     * @param file 文件路径
     * @return
     */
    public static Drawable path2Drawable(Context context, String file) {

        if (file == null || file.isEmpty()) {
            return null;
        }

        Drawable drawable = null;
        try {
            FileInputStream fis = new FileInputStream(file);
            Bitmap bitmap = BitmapFactory.decodeStream(fis);
            drawable = new BitmapDrawable(context.getResources(), bitmap);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        return drawable;
    }
posted @ 2022-05-19 18:14  指切  阅读(1010)  评论(0编辑  收藏  举报