乐闻世界logo
搜索文章和话题

How to load local files/images that are stored in emulator/phone storage in HarmonyOS?

1个答案

1

Loading local files and images stored in emulator or phone storage in HarmonyOS can be accomplished through several methods. Here, I will demonstrate how to load an image file with a specific example. HarmonyOS is developed using Java, so handling files and images is similar to Android, but with unique APIs and framework structures. Here is a step-by-step approach:

Step 1: Add Permissions

First, ensure your application has permission to access the device's storage. In the config.json file, add permissions for reading and writing files:

json
{ "reqPermissions": [ { "name": "ohos.permission.READ_MEDIA" }, { "name": "ohos.permission.WRITE_MEDIA" } ], "deviceConfig": { "default": { "orientation": "unspecified" } } }

Step 2: Define ImageView in Layout File

In your interface layout XML file, define an ImageView component to display the loaded image:

xml
<?xml version="1.0" encoding="utf-8"?> <ohos.agp.components.ComponentContainer xmlns:ohos="http://schemas.huawei.com/res/ohos" ohos:height="match_parent" ohos:width="match_parent"> <ohos.agp.components.Image ohos:id="$+id:image_view" ohos:width="match_content" ohos:height="match_content" ohos:top_margin="50vp" ohos:layout_alignment="center"/> </ohos.agp.components.ComponentContainer>

Step 3: Load Image Using Java Code

In your Activity or Ability (components in HarmonyOS are analogous to Activities in Android), use the following Java code to load image files from storage:

java
package com.example.harmonyosapp; import ohos.aafwk.ability.Ability; import ohos.aafwk.content.Intent; import ohos.agp.components.Image; import ohos.agp.components.element.PixelMapElement; import ohos.media.image.ImageSource; import ohos.media.image.PixelMap; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; public class MainAbility extends Ability { @Override public void onStart(Intent intent) { super.onStart(intent); super.setUIContent(ResourceTable.Layout_ability_main); Image imageView = (Image) findComponentById(ResourceTable.Id_image_view); try { File file = new File(getFilesDir(), "example.jpg"); FileInputStream fileInputStream = new FileInputStream(file); ImageSource imageSource = ImageSource.create(fileInputStream, null); PixelMap pixelMap = imageSource.createPixelmap(null); imageView.setPixelMap(pixelMap); fileInputStream.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }

In the above code, the getFilesDir() method retrieves the application's private directory path. Next, create a File object using the filename and a FileInputStream from it. Then, use ImageSource.create() to load the image from the file input stream and convert it to a PixelMap, which is finally set to the ImageView.

This example demonstrates loading image files from HarmonyOS device storage and displaying them in the user interface. Note that when handling files and images, consider permissions and error handling to ensure application robustness and user experience.

2024年7月26日 22:27 回复

你的答案