Jaswanth
Jaswanth

Reputation: 31

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

I am building a HarmonyOS application and want to load an image that I have in emulator/phone storage. How can I load this image in my Java class?

Upvotes: 1

Views: 233

Answers (1)

zhangxaochen
zhangxaochen

Reputation: 34007

To load the system images in HarmonyOS, you can obtain the absolute path of an image in the following ways:

  1. Configure the following permissions in the config.json file:
"reqPermissions": [

 {

"name": "ohos.permission.READ_USER_STORAGE"

 },

 {

"name": "ohos.permission.WRITE_USER_STORAGE"

 },

 {

"name": "ohos.permission.GET_BUNDLE_INFO"

 }

]
  1. Manually request permission to access photos and media on the device from the user in MainAbility.java:
public class MainAbility extends Ability {

   @Override

   public void onStart(Intent intent) {

       super.onStart(intent);

       super.setMainRoute(MainAbilitySlice.class.getName());

       requestPermission();

   }

   private void requestPermission() {

       String[] permissions = {

               "ohos.permission.READ_USER_STORAGE",

               "ohos.permission.WRITE_USER_STORAGE",

               "ohos.permission.DISTRIBUTED_DATASYNC"

       };

       List<String> applyPermissions = new ArrayList<>();

       for (String element : permissions) {

           if (verifySelfPermission(element) != IBundleManager.PERMISSION_GRANTED) {

               if (canRequestPermission(element)) {

                   applyPermissions.add(element);

               }

           }

       }

       requestPermissionsFromUser(applyPermissions.toArray(new String[0]), 0);

   }

}

3.The MainAbilitySlice.java code is as follows:

public class MainAbilitySlice extends AbilitySlice {

private static final HiLogLabel TAG = new HiLogLabel(3, 0xD001100, "MainAbilitySlice");

   @Override

   public void onStart(Intent intent) {

       super.onStart(intent);

       super.setUIContent(ResourceTable.Layout_ability_main);

     

       // Obtains the absolute path of the system image.

DataAbilityHelper helper = DataAbilityHelper.creator(this);

       ResultSet resultSet;

       try {

           resultSet = helper.query(AVStorage.Images.Media.EXTERNAL_DATA_ABILITY_URI, new String[]

                   {AVStorage.Images.Media.ID, AVStorage.Images.Media.DISPLAY_NAME, AVStorage.Images.Media.DATA}, null);

           while (resultSet != null && resultSet.goToNextRow()) {

               // Obtains the absolute path of the image.

               String path = resultSet.getString(resultSet.getColumnIndexForName(AVStorage.Images.Media.DATA));

           }

       } catch (DataAbilityRemoteException e) {

           HiLog.error(TAG, "DataAbilityRemoteException e" + e);

       }

   }

   @Override

   public void onActive() {

       super.onActive();

   }

   @Override

   public void onForeground(Intent intent) {

       super.onForeground(intent);

   }

}

Upvotes: 1

Related Questions