选择图库图片后,再次选择图库会保留上次选择结果

​在三方获取图库图片时候,当选完图片返回后如果需要再次添加选择更多图片。想再此打开图库选择图片时,会将已经选择的图片信息传递给媒体库接口,让媒体库将图片状态变为已选择。

HarmonyOS
2024-05-21 20:51:25
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
pumayze

使用的核心API

ohos.file.picker
  • 1.

核心代码介绍

@Entry 
@Component 
struct Index { 
  @State message: string = ''; 
  private resultUri: string[] = []; 
  @State mediaItems: string[] = [] 
  @State maxSelectCount: string = '0' 
  scroller: Scroller = new Scroller() 
  @State isShowSerialNum: boolean = false; 
  
  private context: common.UIAbilityContext = getContext() as common.UIAbilityContext; 
  
  
  aboutToAppear(){ 
    this.setPreferredOrientation( 
      // AppStorage.get<common.UIAbilityContext>('photosAbilityContext') as common.UIAbilityContext, 
      this.context, 
      window.Orientation.AUTO_ROTATION_RESTRICTED); 
  } 
  
  setPreferredOrientation(context: common.UIAbilityContext, orientation: number): void { 
    try { 
      window.getLastWindow(context, (err, data) => { 
        if (err.code || !data) { 
          // Log.error(TAG, 'Failed to obtain the top window. Cause: ' + JSON.stringify(err)); 
          return; 
        } 
        let windowClass = data; 
        windowClass.setPreferredOrientation(orientation, (err) => { 
          if (err.code) { 
            // Log.error(TAG, 'Failed to set window orientation. Cause: ' + JSON.stringify(err)); 
            return; 
          } 
          // Log.info(TAG, 'Succeeded in setting window orientation.'); 
        }); 
      }); 
    } catch (exception) { 
      // Log.error(TAG, 'Failed to set window orientation. Cause: ' + JSON.stringify(exception)); 
    } 
  } 
  
  private firstEnter() { 
    this.multiPick().then((result: ability.AbilityResult) => { 
      // Log.info(TAG, `AbilityResult = ${JSON.stringify(result)}`); 
      let want: Want | null = result.want as Want; 
      if (want == null || want.parameters == null) return; 
      this.resultUri = want.parameters['select-item-list'] as string[]; 
      // Log.info(TAG, `item list= ${JSON.stringify(this.resultUri)}`); 
      let length = this.resultUri.length; 
      this.message = `已选择 ${length} 张图片`; 
      this.mediaItems = [...this.resultUri] 
    }); 
  } 
  
  private secondEnter() { 
    this.multiPickWithData().then((result: ability.AbilityResult) => { 
      // Log.info(TAG, `AbilityResult = ${JSON.stringify(result)}`); 
      if (result.resultCode === -1) { 
        this.message = '' 
        this.resultUri = [] 
      } 
      let want: Want | null = result.want as Want; 
      if (want == null || want.parameters == null) return; 
      this.resultUri = want.parameters['select-item-list'] as string[]; 
      // Log.info(TAG, `item list= ${JSON.stringify(this.resultUri)}`); 
      let length = this.resultUri.length; 
      this.message = `已选择 ${length} 张图片`; 
      this.mediaItems = [...this.resultUri] 
    }); 
  } 
  
  
  @Builder showMediaItems() { 
    Column() { 
      Grid(this.scroller) { 
        ForEach(this.mediaItems, (item: string) => { 
          GridItem() { 
            if (item.endsWith('mp4')) { 
              Video({ src: item }) 
                .width('100%') 
                .height('100%') 
            } else { 
              Image(item) 
                .width('100%') 
                .height('100%') 
            } 
          } 
          .width('100%') 
          .aspectRatio(1) 
        }, (item: string) => item) 
      } 
      .columnsTemplate('1fr 1fr 1fr 1fr') 
      .columnsGap(10) 
      .rowsGap(10) 
      .onScrollIndex((first: number) => { 
        console.info(first.toString()) 
      }) 
      .height('100%') 
      .width('90%') 
    } 
    .width('100%') 
    .height(350) 
    .padding({ top: 10 }) 
  } 
  
  build() { 
    Column() { 
      Column() { 
        Text('模拟第三方应用') 
          .fontSize(40) 
      } 
  
      Column() { 
        Text(this.message) 
          .fontSize(30) 
          .fontWeight(FontWeight.Bold) 
      } 
  
      Column() { 
        Row() { 
          Text('最大选择数量: ') 
          TextInput({ placeholder: '请输入最大选择数量', text: this.maxSelectCount }) 
            .width(150) 
            .onChange((value: string) => { 
              this.maxSelectCount = value 
            }) 
        } 
      }.margin({ bottom: 10 }) 
  
      Column() { 
        Row() { 
          Text('是否显示序号: ') 
          Toggle({ type: ToggleType.Switch, isOn: false }) 
            .onChange((isShowSerialNum: boolean) => { 
              this.isShowSerialNum = isShowSerialNum 
            }) 
        } 
      }.margin({ bottom: 10 }) 
      Column() { 
        Button('选择') 
          .width(200) 
          .onClick(() => { 
            this.resultUri = this.resultUri?.length ? this.resultUri : [] 
            if (this.resultUri.length === 0) { 
              this.firstEnter() 
            } else { 
              this.secondEnter() 
            } 
          }) 
      } 
      .width("100%") 
      .justifyContent(FlexAlign.Center) 
      .alignItems(HorizontalAlign.Center) 
  
      // this.showMediaItems() 
    } 
    .height('100%') 
    .width('100%') 
    .padding({bottom:30}) 
  } 
  
  async singlePick(): Promise<ability.AbilityResult> { 
    // let context: common.UIAbilityContext = AppStorage.get<common.UIAbilityContext>('formContext') as common.UIAbilityContext; 
    let param: Want = { 
      bundleName: 'com.huawei.hmos.photos', 
      abilityName: 'com.huawei.hmos.photos.MainAbility', 
      parameters: { 
        uri: WANT_PARAM_URI_SELECT_SINGLE, 
      } 
    }; 
    // Log.debug(TAG, `routeToTimelinePage parm: ${JSON.stringify(param)}`); 
    let result = await this.context.startAbilityForResult(param); 
    return result; 
  } 
  
  async multiPick(): Promise<ability.AbilityResult> { 
    // let context: common.UIAbilityContext = AppStorage.get<common.UIAbilityContext>('formContext') as common.UIAbilityContext; 
    let param: Want = { 
      bundleName: 'com.huawei.hmos.photos', 
      abilityName: 'com.huawei.hmos.photos.MainAbility', 
      parameters: { 
        uri: WANT_PARAM_URI_SELECT_MULTIPLE, 
        preData: this.resultUri, 
        maxSelectCount: this.maxSelectCount, 
        isShowSerialNum: this.isShowSerialNum 
      } 
    }; 
    // Log.debug(TAG, `routeToTimelinePage parm: ${JSON.stringify(param)}`); 
    let result = await this.context.startAbilityForResult(param); 
    return result; 
  } 
  
  async multiPickWithData(): Promise<ability.AbilityResult> { 
    // let context: common.UIAbilityContext = AppStorage.get<common.UIAbilityContext>('formContext') as common.UIAbilityContext; 
    // Log.debug(TAG, `multiPickWithData`); 
    let param: Want = { 
      bundleName: 'com.huawei.hmos.photos', 
      abilityName: 'com.huawei.hmos.photos.MainAbility', 
      parameters: { 
        uri: WANT_PARAM_URI_SELECT_MULTIPLE, 
        preData: this.resultUri, 
        maxSelectCount: this.maxSelectCount, 
        isShowSerialNum: this.isShowSerialNum 
      } 
    }; 
    // Log.debug(TAG, `multiPickWithData parm: ${JSON.stringify(param)}`); 
    let result = await this.context.startAbilityForResult(param); 
    return result; 
  } 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.
  • 160.
  • 161.
  • 162.
  • 163.
  • 164.
  • 165.
  • 166.
  • 167.
  • 168.
  • 169.
  • 170.
  • 171.
  • 172.
  • 173.
  • 174.
  • 175.
  • 176.
  • 177.
  • 178.
  • 179.
  • 180.
  • 181.
  • 182.
  • 183.
  • 184.
  • 185.
  • 186.
  • 187.
  • 188.
  • 189.
  • 190.
  • 191.
  • 192.
  • 193.
  • 194.
  • 195.
  • 196.
  • 197.
  • 198.
  • 199.
  • 200.
  • 201.
  • 202.
  • 203.
  • 204.
  • 205.
  • 206.
  • 207.
  • 208.
  • 209.
  • 210.

实现效果

适配版本信息

DevEco Studio Version: 4.1.1.400

SDK:HarmoneyOS 4.1.0.25

分享
微博
QQ
微信
回复
2024-05-22 16:59:53
相关问题
HarmonyOS 图库选择
796浏览 • 1回复 待解决
HarmonyOS H5调用APP图库选择图片
856浏览 • 1回复 待解决
HarmonyOS 本地图库选择器并编辑图片
722浏览 • 1回复 待解决
HarmonyOS web如何拉起图库选择
943浏览 • 1回复 待解决