卸载应用之后仍能获取到数据的方式?

1.写入

let attr: asset.AssetMap = new Map();
attr.set(asset.Tag.SECRET, this.stringToArray(‘demo_pwd’));
attr.set(asset.Tag.ALIAS, this.stringToArray(‘demo_alias’));
attr.set(asset.Tag.ACCESSIBILITY, asset.Accessibility.DEVICE_FIRST_UNLOCKED);
attr.set(asset.Tag.IS_PERSISTENT,true);
attr.set(asset.Tag.DATA_LABEL_NORMAL_1, this.stringToArray(‘demo_label’));
try {
  asset.add(attr).then(() => {
    console.info(Asset added successfully.);
  }).catch(() => {
    console.error(Failed to add Asset.);
  })
} catch (error) {
  console.error(Failed to add Asset.);
}

let attr: asset.AssetMap = new Map();
attr.set(asset.Tag.SECRET, stringToArray(‘demo_pwd’));
attr.set(asset.Tag.ALIAS, stringToArray(‘demo_alias’));
attr.set(asset.Tag.ACCESSIBILITY, asset.Accessibility.DEVICE_FIRST_UNLOCKED);
attr.set(asset.Tag.DATA_LABEL_NORMAL_1, stringToArray(‘demo_label’));
try {
  asset.add(attr).then(() => {
    console.info(Asset added successfully.);
  }).catch(() => {
    console.error(Failed to add Asset.);
  })
} catch (error) {
  console.error(Failed to add Asset.);
}

2.查询
let query: asset.AssetMap = new Map();
query.set(asset.Tag.ALIAS, this.stringToArray(‘demo_alias’)); // 指定了关键资产别名,最多查询到一条满足条件的关键资产
query.set(asset.Tag.RETURN_TYPE, asset.ReturnType.ALL); // 此处表示需要返回关键资产的所有信息,即属性+明文
try {
  asset.query(query).then((res: Array<asset.AssetMap>) => {
    for (let i = 0; i < res.length; i++) {
      // parse the secret.
      let secret: Uint8Array = res[i].get(asset.Tag.SECRET) as Uint8Array;
      // parse uint8array to string
      let secretStr: string = this.arrayToString(secret);
      console.error(get secretStr from query Asset. + secretStr);
    }
  }).catch (() => {
    console.error(Failed to query Asset.);
  });
} catch (error) {
  console.error(Failed to query Asset.);
}
}
以上两个接口都会调用到 catch 里面去 偶尔成功。
阅读 738
1 个回答

代码catch部分麻烦可以增加一个error参数打印增加的错误码信息,

asset.add(attr).then(() => {
  console.info(Asset added successfully.);
}).catch((error: Error) => {
  console.error(Failed to add Asset. + error);
})

通过error就可以获取到具体的错误信息,进行下一步操作。

具体错误信息可参考官网API指导或关键资产存储服务(ASSET)错误码链接查看官网API指导链接:https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-asset-0000001863796493

关键资产存储服务(ASSET)错误码链接:https://developer.huawei.com/consumer/cn/doc/harmonyos-references/errorcode-asset-0000001816916934

catch是因为您调用asset内的方法有步骤不满足条件或有问题,这时候可通过捕获的异常信息进行下一操作或提醒。例如在将写入操作改成下面代码:

Button(‘写入’).onClick(() => {
  let attr: asset.AssetMap = new Map();
  attr.set(asset.Tag.SECRET, stringToArray(‘demo_pwd’));
  attr.set(asset.Tag.ALIAS, stringToArray(‘demo_alias’));
  attr.set(asset.Tag.IS_PERSISTENT, true);
  attr.set(asset.Tag.ACCESSIBILITY, asset.Accessibility.DEVICE_FIRST_UNLOCKED);
  attr.set(asset.Tag.DATA_LABEL_NORMAL_1, stringToArray(‘demo_label’));
  try {
    asset.add(attr).then(() => {
      console.info(Asset added successfully.);
    }).catch((error: Error) => {
      console.error(Failed to add Asset. + error);
    })
  } catch (error) {
    console.error(Failed to add Asset.);
  }
})

无权限说明开发者使用,权限需在HarmonyProject\entry\src\main路径下找到module.json5 在其内申请 ohos.permission.STORE\_PERSISTENT\_DATA 权限如在合适位置添加如下代码:

“requestPermissions”: [
{
  “name”: “ohos.permission.STORE_PERSISTENT_DATA”
}
]

也可参考声明权限链接去申请此权限声明权限链接:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/declare-permissions-0000001820999665

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进