首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从HarmonyOS中的资源设置PixelMap?

如何从HarmonyOS中的资源设置PixelMap?
EN

Stack Overflow用户
提问于 2021-07-27 21:20:20
回答 2查看 71关注 0票数 1

在安卓中,我们可以使用BitmapFactory为资源图像/可绘制创建一个位图:

代码语言:javascript
复制
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.image);

如何从HarmonyOS中的资源id创建相应的PixelMap?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-07-28 13:12:26

欢迎加入社区!

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

代码语言:javascript
复制
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, "%{public}s: %{public}s", new Object[]{"getPixelMap", "IOException"});
        }
        return Optional.empty();
    }
}
票数 0
EN

Stack Overflow用户

发布于 2021-07-28 17:27:35

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

代码语言:javascript
复制
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();

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68545645

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档