鴻蒙HarmonyOS-獲取系統(tǒng)照片并解碼渲染顯示2(附更完整的Demo)
51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)
https://harmonyos.51cto.com/#zz
聲明一下哦,本篇是接著我的上一篇文章——#2020征文-手機(jī)#獲取系統(tǒng)照片并解碼渲染顯示(附完整demo) 原創(chuàng) 來寫的。需要的可以先讀讀上一篇文件滴,本篇?jiǎng)t是在上一篇代碼基礎(chǔ)上進(jìn)一步修改而來。
說一下功能的升級(較上一版本):(ps:我也想搞分布式,可目前的現(xiàn)實(shí)不允許,還是等遠(yuǎn)程模擬器的多設(shè)備分布式聯(lián)調(diào)能力開放吧)
1.沒有圖片會(huì)出現(xiàn)提示
2.相冊中的所有照片都可顯示,并且顯示計(jì)數(shù)
3.應(yīng)用隨打開隨刷新
不多說,先上demo跑起來的效果,如下兩張圖:第一張圖是在手機(jī)遠(yuǎn)程模擬器中一張圖片都沒有時(shí)候的顯示界面,第二張是自己打開遠(yuǎn)程模擬器的照相功能一頓亂點(diǎn),照了N張之后的顯示界面
完整的demo在附件中進(jìn)行下載

老規(guī)矩先說升級的大概思路:
1.采用TableLayout布局實(shí)現(xiàn)了所有照片的顯示
2.添加兩個(gè)Text用來顯示無照片的提示信息和照片的計(jì)數(shù)信息
3.在onActive生命周期函數(shù)中添加方法實(shí)現(xiàn)實(shí)時(shí)刷新
1.采用TableLayout布局實(shí)現(xiàn)了所有照片的顯示
1.1 在布局文件中添加TableLayout布局代碼,需要注意的是:這里我外邊套了一層ScrollView,這是為了在圖片多的時(shí)候,TableLayout可以滑動(dòng)
- <ScrollView ohos:width="match_parent"
- ohos:height="600vp"
- ohos:left_padding="25vp"
- >
- <TableLayout
- ohos:id="$+id:layout_id"
- ohos:height="match_content"
- ohos:width="match_parent"
- >
- </TableLayout>
- </ScrollView>
1.2 在java代碼中獲取到這個(gè)布局
- TableLayout img_layout;
- img_layout = (TableLayout)findComponentById(ResourceTable.Id_layout_id);
- img_layout.setColumnCount(3);
1.3 將新生成的圖片放入布局中
- Image img = new Image(MainAbilitySlice.this);
- img.setId(mediaId);
- img.setHeight(300);
- img.setWidth(300);
- img.setMarginTop(20);
- img.setMarginLeft(20);
- img.setPixelMap(pixelMap);
- img.setScaleMode(Image.ScaleMode.ZOOM_CENTER);
- img_layout.addComponent(img);
2.添加兩個(gè)Text用來顯示無照片的提示信息和照片的計(jì)數(shù)信息
2.1 首先在布局文件中加入兩個(gè)text
- <Text
- ohos:id="$+id:text_pre_id"
- ohos:width="match_parent"
- ohos:height="match_parent"
- ohos:text_alignment="center"
- ohos:text_size="45fp"
- ohos:text="Opening..."></Text>
- <Text
- ohos:id="$+id:text_id"
- ohos:width="match_content"
- ohos:height="match_content"
- ohos:text_alignment="center"
- ohos:text_size="20fp"></Text>
2.2 在java中獲得這兩個(gè)text組件
- Text pre_text,text;
- pre_text = (Text)findComponentById(ResourceTable.Id_text_pre_id);
- text = (Text)findComponentById(ResourceTable.Id_text_id);
2.3 利用能不能獲取到圖片來判斷這兩個(gè)text組件的顯示邏輯
- if(img_ids.size() > 0){
- pre_text.setVisibility(Component.HIDE);
- text.setVisibility(Component.VISIBLE);
- text.setText("照片數(shù)量:"+img_ids.size());
- }else{
- pre_text.setVisibility(Component.VISIBLE);
- pre_text.setText("No picture.");
- text.setVisibility(Component.HIDE);
- }
3.在onActive生命周期函數(shù)中添加方法實(shí)現(xiàn)實(shí)時(shí)刷新
3.1 onActive生命周期函數(shù)介紹
- Page會(huì)在進(jìn)入INACTIVE狀態(tài)后來到前臺(tái),然后系統(tǒng)調(diào)用此回調(diào)。Page在此之后進(jìn)入ACTIVE狀態(tài),該狀態(tài)是應(yīng)用與用戶交互的狀態(tài)。所以當(dāng)你把應(yīng)用放到后臺(tái),打開照相機(jī)照相的時(shí)候,然后在打開此應(yīng)用的時(shí)候就會(huì)調(diào)用該生命周期函數(shù)
3.2 在onActive函數(shù)中添加需要的調(diào)用
- @Override
- public void onActive() {
- super.onActive();
- displayPic();
- }
3.3 displayPic函數(shù)封裝了整個(gè)展示圖片的代碼
- public void displayPic(){
- img_layout.removeAllComponents();
- ArrayList<Integer> img_ids = new ArrayList<Integer>();
- DataAbilityHelper helper = DataAbilityHelper.creator(getContext());
- try {
- ResultSet result = helper.query(AVStorage.Images.Media.EXTERNAL_DATA_ABILITY_URI, null, null);
- if(result == null){
- pre_text.setVisibility(Component.VISIBLE);
- }else{
- pre_text.setVisibility(Component.HIDE);
- }
- while(result != null && result.goToNextRow()){
- int mediaId = result.getInt(result.getColumnIndexForName(AVStorage.Images.Media.ID));
- Uri uri = Uri.appendEncodedPathToUri(AVStorage.Images.Media.EXTERNAL_DATA_ABILITY_URI,""+mediaId);
- FileDescriptor filedesc = helper.openFile(uri,"r");
- ImageSource.DecodingOptions decodingOpts = new ImageSource.DecodingOptions();
- decodingOpts.desiredSize = new Size(300,300);
- ImageSource imageSource = ImageSource.create(filedesc,null);
- PixelMap pixelMap = imageSource.createThumbnailPixelmap(decodingOpts,true);
- Image img = new Image(MainAbilitySlice.this);
- img.setId(mediaId);
- img.setHeight(300);
- img.setWidth(300);
- img.setMarginTop(20);
- img.setMarginLeft(20);
- img.setPixelMap(pixelMap);
- img.setScaleMode(Image.ScaleMode.ZOOM_CENTER);
- img_layout.addComponent(img);
- System.out.println("xxx"+uri);
- img_ids.add(mediaId);
- }
- }catch (DataAbilityRemoteException | FileNotFoundException e){
- e.printStackTrace();
- }
- if(img_ids.size() > 0){
- pre_text.setVisibility(Component.HIDE);
- text.setVisibility(Component.VISIBLE);
- text.setText("照片數(shù)量:"+img_ids.size());
- }else{
- pre_text.setVisibility(Component.VISIBLE);
- pre_text.setText("No picture.");
- text.setVisibility(Component.HIDE);
- }
- }
這個(gè)demo目前來說,還算基本能看。。。有時(shí)間的我會(huì)繼續(xù)嘗試修改完善。
有興趣的朋友可以關(guān)注一下
完整demo的源碼見附件
©著作權(quán)歸作者和HarmonyOS技術(shù)社區(qū)共同所有,如需轉(zhuǎn)載,請注明出處,否則將追究法律責(zé)任
51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)
https://harmonyos.51cto.com/#zz