微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何从鸿蒙系统中的资源设置 PixelMap?

如何解决如何从鸿蒙系统中的资源设置 PixelMap?

在 Android 中,我们可以使用 BitmapFactory 为资源图像/可绘制对象创建位图:

Bitmap bm = BitmapFactory.decodeResource(getResources(),R.drawable.image);

我们如何在鸿蒙系统中根据资源 id 创建相应的 PixelMap?

解决方法

欢迎加入社区!

我们可以通过以下方法从资源中获取 PixelMap。

import ohos.app.Context;
import ohos.global.resource.NotExistException;
import ohos.global.resource.RawFileEntry;
import ohos.global.resource.Resource;
import ohos.global.resource.ResourceManager;
import ohos.global.resource.WrongTypeException;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
import java.io.IOException;
import java.util.Optional;

/**
 * Resources Utility 
 */
public class ResUtil {
    private static final String LABEL = "ResUtil";
    private static final String MESSAGE = "IOException | NotExistException | WrongTypeException";
    private static final HiLogLabel LABEL_LOG = new HiLogLabel(3,123401234,"ResUtil");

    private ResUtil() {
    }

    /**
     * get the path from id.
     *
     * @param context the context
     * @param id      the id
     * @return the path from id
     */
    public static String getPathById(Context context,int id) {
        String path = "";
        if (context == null) {
            return path;
        }
        ResourceManager manager = context.getResourceManager();
        if (manager == null) {
            return path;
        }
        try {
            path = manager.getMediaPath(id);
        } catch (IOException | NotExistException | WrongTypeException e) {
            HiLog.error(LABEL_LOG,"%{public}s: %{public}s",new Object[]{"getPathById",MESSAGE});
        }
        return path;
    }

    /**
     * check if the input string is empty.
     *
     * @param input string
     * @return boolean value
     */
    public static boolean isEmpty(String input) {
        return input == null || input.length() == 0;
    }

    /**
     * get the pixel map.
     *
     * @param context the context
     * @param id      resource id
     * @return the pixel map
     */
    public static Optional<PixelMap> getPixelMap(Context context,int id) {
        String path = getPathById(context,id);
        if (context == null || isEmpty(path)) {
            return Optional.empty();
        }
        RawFileEntry assetManager = context.getResourceManager().getRawFileEntry(path);
        ImageSource.SourceOptions options = new ImageSource.SourceOptions();
        options.formatHint = "image/png";
        ImageSource.DecodingOptions decodingOptions = new ImageSource.DecodingOptions();
        try {
            Resource asset = assetManager.openRawFile();
            ImageSource source = ImageSource.create(asset,options);
            return Optional.ofNullable(source.createPixelmap(decodingOptions));
        } catch (IOException e) {
            HiLog.error(LABEL_LOG,new Object[]{"getPixelMap","IOException"});
        }
        return Optional.empty();
    }
}
,

也许你可以试试下面的代码:

try {

    Resource resource = getResourceManager().getResource(ResourceTable.Media_icon);

    PixelMapElement pixelMapElement = new PixelMapElement(resource);

    PixelMap pixelMap = pixelMapElement.getPixelMap();

} catch (IOException e) {

    e.printStackTrace();

} catch (NotExistException e) {

    e.printStackTrace();

}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。