applibgroup / HarmonyOS-Knowledgebase

This repository contains code samples of HarmonyOS Training
Apache License 2.0
17 stars 6 forks source link

How to set up a PixelMap from resource in HarmonyOS? #16

Closed sohamvg closed 3 years ago

sohamvg commented 3 years ago

Describe the query

In Android, we can create a Bitmap for a resource image/drawable using BitmapFactory:

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

How can we create a corresponding PixelMap from a resource id in HarmonyOS?

Create the query with harmonyos tag in stackoverflow and share the link here:

https://stackoverflow.com/questions/68545645/how-to-set-up-a-pixelmap-from-resource-in-harmonyos

Additional information

Developer Platform: Windows DevEco Studio version: 2.1.0.303 SDK API version: 5 SDK version: 2.1.1.21 Device: Not Required Device OS version: Not Required

kanaksony commented 3 years ago

Answered and accepted on Stackoverflow, so closing.

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();
    }
}