自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

基于鴻蒙分布式跨設(shè)備文件服務(wù)-信件管理

開發(fā) 前端 分布式
這個跨設(shè)備信件管理應(yīng)用,A手機(jī)創(chuàng)建一封信件,生成圖片,在A手機(jī)顯示本地端, 在B手機(jī)顯示遠(yuǎn)程端, 同時A,B手機(jī)都可以打開查看信件內(nèi)容,這里使用到了分布式數(shù)據(jù)庫管理,使用列表存儲圖片名.

[[431770]]

想了解更多內(nèi)容,請?jiān)L問:

51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)

https://harmonyos.51cto.com

一. 前言

先說說寫這個跨設(shè)備文件服務(wù)信件管理應(yīng)用前, 都想過做什么, 首先看了梁迪迪的基于分布式文件服務(wù)的文本編輯器,也想做一個文檔的,比如創(chuàng)建word,excel,pdf文件,然后點(diǎn)擊可以打開WPS軟件來編輯,可惜搜索了網(wǎng)上找不到打開WPS方法,最后放棄了;然后想到弄一個畫板,在畫板上畫上自己想表達(dá)的內(nèi)容,保存為圖片,跨設(shè)備可以打開圖片查看,開始時保存圖片,想用截屏的方法,查看文檔沒有找到Java調(diào)用系統(tǒng)的截屏方式,看到了JS的,但是要API7才支持,最后也放棄了,然而腦子里一閃,想起以前讀書時,自習(xí)課不用大聲說話,很多同學(xué)都是通過傳紙條,那時也流行寫信件,就往這個想法開始擼碼,這里有幾個知識點(diǎn),之前沒有寫過,比如怎么把文本框輸入的內(nèi)容寫到信紙上,然后保存為圖片,在另一臺設(shè)備上點(diǎn)擊圖片,可以查看里面的內(nèi)容,通過網(wǎng)上搜索,還是找到相似的知識點(diǎn), 才能在今天里做出了這個跨設(shè)備查看文件應(yīng)用.

先簡單說一下這個跨設(shè)備信件管理應(yīng)用,A手機(jī)創(chuàng)建一封信件,生成圖片,在A手機(jī)顯示本地端, 在B手機(jī)顯示遠(yuǎn)程端, 同時A,B手機(jī)都可以打開查看信件內(nèi)容,這里使用到了分布式數(shù)據(jù)庫管理,使用列表存儲圖片名,方便列表顯示出來,然后點(diǎn)擊相應(yīng)的圖片,獲取到圖片名,再到分布式文件路徑獲取到圖片,顯示出來.

二. 實(shí)現(xiàn)效果

開發(fā)工具環(huán)境下視頻:https://www.bilibili.com/video/BV16L4y1i7b1/

手機(jī)+手機(jī)環(huán)境下視頻:https://www.bilibili.com/video/BV1mL411g72B/

三. 創(chuàng)建工程

在這當(dāng)作你已經(jīng)安裝好最新版本DevEco-Studio開發(fā)工具, 點(diǎn)擊File -> New -> New Project… 彈出Create HarmonyOS Project窗口, 這里我選擇空白Java模板創(chuàng)建, 上一個視頻播放實(shí)例是用JS寫的界面,這個跨設(shè)備信件管理界面就用Java來寫,還是JS寫界面快,調(diào)試也快些.

基于鴻蒙分布式跨設(shè)備文件服務(wù)-信件管理-鴻蒙HarmonyOS技術(shù)社區(qū)

Java模塊布局模塊

四. 主界面開發(fā)

先介紹公共類Java代碼,有了這些公共類,以后做類似功能的應(yīng)用,可以直接復(fù)制公共類文件可以使用:

DistributedFileUtil 分布式文件工具類:

  1. package com.army.study.util; 
  2.  
  3. import com.army.study.ResourceTable; 
  4. import ohos.agp.render.Canvas; 
  5. import ohos.agp.render.Paint; 
  6. import ohos.agp.render.Texture; 
  7. import ohos.agp.utils.Color; 
  8. import ohos.app.Context; 
  9. import ohos.global.resource.NotExistException; 
  10. import ohos.media.image.ImagePacker; 
  11. import ohos.media.image.ImageSource; 
  12. import ohos.media.image.PixelMap; 
  13. import ohos.media.image.common.Size
  14.  
  15. import java.io.*; 
  16. import java.util.ArrayList; 
  17. import java.util.Arrays; 
  18. import java.util.List; 
  19.  
  20. /** 
  21.  * 分布式文件工具類 
  22.  */ 
  23. public class DistributedFileUtil { 
  24.     // 上下文 
  25.     private final Context mContext; 
  26.  
  27.     /** 
  28.      * 構(gòu)造方法 
  29.      * @param context 
  30.      */ 
  31.     public DistributedFileUtil(Context context) { 
  32.         this.mContext = context; 
  33.     } 
  34.  
  35.     /** 
  36.      * 寫信件 
  37.      * @param fileName 
  38.      * @param letterContent 
  39.      * @return 
  40.      */ 
  41.     public PixelMap writeLetter(String fileName, String letterContent) { 
  42.         // 獲取分布式文件路徑 
  43.         String filePath = mContext.getDistributedDir() + File.separator + fileName + ".jpg"
  44.         Texture texture=null
  45.         try { 
  46.             // 從資源文件獲取信紙背景圖片 
  47.             InputStream inputStream = mContext.getResourceManager().getResource(ResourceTable.Media_bg); 
  48.             ImageSource.SourceOptions srcOpts = new ImageSource.SourceOptions(); 
  49.             srcOpts.formatHint = "image/jpeg"
  50.             ImageSource imageSource = ImageSource.create(inputStream, srcOpts); 
  51.             // 設(shè)置圖片參數(shù) 
  52.             ImageSource.DecodingOptions decodingOptions = new ImageSource.DecodingOptions(); 
  53.             decodingOptions.desiredSize=new Size(720,1080); 
  54.             PixelMap pixelMap = imageSource.createPixelmap(decodingOptions); 
  55.             //用于保存畫圖結(jié)果 
  56.             texture=new Texture(pixelMap); 
  57.             Canvas canvas=new Canvas(texture); 
  58.             Paint paint=new Paint(); 
  59.             paint.setTextSize(50); 
  60.             paint.setStrokeWidth(8); 
  61.             paint.setColor(Color.BLACK); 
  62.             // 把內(nèi)容寫到信紙上 
  63.             canvas.drawChars(paint,letterContent.toCharArray(),50,140); 
  64.             // 文件輸出流 
  65.             FileOutputStream fos=new FileOutputStream(filePath); 
  66.  
  67.             ImagePacker imagePacker = ImagePacker.create(); 
  68.             ImagePacker.PackingOptions packingOptions = new ImagePacker.PackingOptions(); 
  69.             packingOptions.format = "image/jpeg";//只支持image/jpeg 
  70.             packingOptions.quality = 90; 
  71.             boolean result = imagePacker.initializePacking(fos, packingOptions); 
  72.             if(result) 
  73.             { 
  74.                 //這里獲取繪畫后的pixelMap用來保存 
  75.                 result = imagePacker.addImage(texture.getPixelMap()); 
  76.                 if (result) { 
  77.                     long dataSize = imagePacker.finalizePacking(); 
  78.                     System.out.println("文件大小:"+dataSize); 
  79.                     ToastUtil.getInstance().showToast(mContext, "創(chuàng)建成功!"); 
  80.                 } 
  81.             } 
  82.  
  83.             fos.flush(); 
  84.             fos.close(); 
  85.         } catch (IOException | NotExistException e) { 
  86.             System.out.println("文件保存出錯:"+e.getMessage()); 
  87.             e.printStackTrace(); 
  88.         } 
  89.          
  90.         return texture.getPixelMap(); 
  91.     } 
  92.  
  93.     /** 
  94.      * 讀取信件 
  95.      * @param fileName 
  96.      * @param letterContent 
  97.      * @return 
  98.      */ 
  99.     public PixelMap readImage(String fileName, String letterContent) { 
  100.         // 獲取分布式文件路徑 
  101.         String filePath = mContext.getDistributedDir() + File.separator + fileName; 
  102.         // 根據(jù)分布式文件路徑,生成文件 
  103.         File file = new File(filePath); 
  104.         if (!file.exists()) { 
  105.             // 如果文件不存在, 調(diào)用寫信件 
  106.             writeLetter(fileName, letterContent); 
  107.         } 
  108.         // 圖片參數(shù) 
  109.         ImageSource.SourceOptions srcOpts = new ImageSource.SourceOptions(); 
  110.         srcOpts.formatHint = "image/jpeg"
  111.         // 創(chuàng)建圖片源 
  112.         ImageSource imageSource = ImageSource.create(file, srcOpts); 
  113.         // 生成圖片 
  114.         PixelMap pixelMap = imageSource.createPixelmap(null); 
  115.  
  116.         return pixelMap; 
  117.     } 
  118.  
  119.     /** 
  120.      * 獲取文件列表 
  121.      * @return 
  122.      */ 
  123.     public List<String> getFileList() { 
  124.         // 獲取分布式文件列表 
  125.         File[] files = mContext.getDistributedDir().listFiles(); 
  126.         List<File> listFile = new ArrayList<>(Arrays.asList(files)); 
  127.         // 排序文件順序 
  128.         listFile.sort((file, newFile) -> { 
  129.             if (file.lastModified() > newFile.lastModified()) { 
  130.                 return -1; 
  131.             } else if (file.lastModified() == newFile.lastModified()) { 
  132.                 return 0; 
  133.             } else { 
  134.                 return 1; 
  135.             } 
  136.         }); 
  137.         List<String> listFileName = new ArrayList<>(); 
  138.         // 獲取文件列表文件名 
  139.         for (File f : listFile) { 
  140.             if (f.isFile()) { 
  141.                 String name = f.getName(); 
  142.                 listFileName.add(name); 
  143.             } 
  144.         } 
  145.         return listFileName; 
  146.     } 

ToastUtil 提示信息框:

  1. package com.army.study.util; 
  2.  
  3.  
  4. import com.army.study.ResourceTable; 
  5. import ohos.agp.components.Component; 
  6. import ohos.agp.components.LayoutScatter; 
  7. import ohos.agp.components.Text; 
  8. import ohos.agp.window.dialog.ToastDialog; 
  9. import ohos.app.Context; 
  10.  
  11. /** 
  12.  * Toast工具類 
  13.  * 
  14.  */ 
  15. public class ToastUtil { 
  16.     private ToastDialog toastDialog; 
  17.  
  18.     private ToastUtil() { 
  19.     } 
  20.  
  21.     public static ToastUtil getInstance() { 
  22.         return ToastUtilInstance.INSTANCE; 
  23.     } 
  24.  
  25.     private static class ToastUtilInstance { 
  26.         private static final ToastUtil INSTANCE = new ToastUtil(); 
  27.     } 
  28.  
  29.     /** 
  30.      * 顯示Toast 
  31.      * 
  32.      * @param context 
  33.      * @param content 
  34.      */ 
  35.     public void showToast(Context context, String content) { 
  36.         if (toastDialog != null && toastDialog.isShowing()) { 
  37.             toastDialog.cancel(); 
  38.         } 
  39.  
  40.         Component toastLayout = LayoutScatter.getInstance(context) 
  41.                 .parse(ResourceTable.Layout_layout_toast, nullfalse); 
  42.         Text toastText = (Text) toastLayout.findComponentById(ResourceTable.Id_text_msg_toast); 
  43.         toastText.setText(content); 
  44.         toastDialog = new ToastDialog(context); 
  45.         toastDialog.setComponent(toastLayout); 
  46.         toastDialog.setTransparent(true); 
  47.         toastDialog.show(); 
  48.     } 

預(yù)覽信件內(nèi)容:

  1. /** 
  2.  * 預(yù)覽信件內(nèi)容 
  3.  */ 
  4. public class PreviewLetterDialog extends CommonDialog { 
  5.  
  6.     public PreviewLetterDialog(Context context, PixelMap imgId) { 
  7.         super(context); 
  8.         Component container = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_dialog_previce_letter, nullfalse); 
  9.         setContentCustomComponent(container); 
  10.         setSize(MATCH_PARENT, MATCH_CONTENT); 
  11.  
  12.         setCornerRadius(AttrHelper.vp2px(20, context)); 
  13.  
  14.         Image image = (Image) container.findComponentById(ResourceTable.Id_preview); 
  15.         image.setPixelMap(imgId); 
  16.  
  17.  
  18.         Button btnCancel = (Button) container.findComponentById(ResourceTable.Id_button_dialog_create_file_cancel); 
  19.         Button btnConfirm = (Button) container.findComponentById(ResourceTable.Id_button_dialog_create_file_confirm); 
  20.  
  21.         btnCancel.setClickedListener(component -> { destroy();}); 
  22.         btnConfirm.setClickedListener(component -> { destroy();}); 
  23.     } 
  24.  

寫信件對話框:

  1. /** 
  2.  * 寫信件對話框 
  3.  */ 
  4. public class CreateLetterDialog extends CommonDialog { 
  5.     private OnCallBack onCallBack; 
  6.  
  7.     public CreateLetterDialog(Context context) { 
  8.         super(context); 
  9.         Component container = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_dialog_write_letter, nullfalse); 
  10.         setContentCustomComponent(container); 
  11.  
  12.         Optional<Display> display = DisplayManager.getInstance().getDefaultDisplay(context); 
  13.         int width = (int) (display.get().getAttributes().width * 0.9); 
  14.         int height = AttrHelper.vp2px(270, context); 
  15.         setSize(width, height); 
  16.         setCornerRadius(AttrHelper.vp2px(20, context)); 
  17.  
  18.         TextField letterContent = (TextField) container.findComponentById(ResourceTable.Id_tf_dialog_create_file_name); 
  19.         Button btnCancel = (Button) container.findComponentById(ResourceTable.Id_button_dialog_create_file_cancel); 
  20.         Button btnConfirm = (Button) container.findComponentById(ResourceTable.Id_button_dialog_create_file_confirm); 
  21.         btnConfirm.setEnabled(false); 
  22.         btnConfirm.setAlpha(0.5f); 
  23.         letterContent.addTextObserver((text, i, i1, i2) -> { 
  24.             if(text.isEmpty()){ 
  25.                 btnConfirm.setEnabled(false); 
  26.                 btnConfirm.setAlpha(0.5f); 
  27.             }else
  28.                 btnConfirm.setEnabled(true); 
  29.                 btnConfirm.setAlpha(1f); 
  30.             } 
  31.         }); 
  32.  
  33.         btnCancel.setClickedListener(component -> { destroy();}); 
  34.  
  35.         btnConfirm.setClickedListener(component -> { 
  36.             if(onCallBack!=null){ 
  37.                 // 設(shè)備ID 
  38.                 String deviceID = KvManagerFactory.getInstance().createKvManager(new KvManagerConfig(context)) 
  39.                         .getLocalDeviceInfo().getId(); 
  40.                 // 組合文件名,方便區(qū)分是否為當(dāng)前設(shè)備創(chuàng)造的文件 
  41.                 String name = deviceID + "-" + letterContent.getText(); 
  42.                 onCallBack.onConfirm(name); 
  43.             } 
  44.             destroy(); 
  45.         }); 
  46.     } 
  47.  
  48.     public void setOnCallBack(OnCallBack onCallBack) { 
  49.         this.onCallBack = onCallBack; 
  50.     } 
  51.  
  52.     public interface OnCallBack { 
  53.         void onConfirm(String name); 
  54.     } 

 主界面代碼圖:

基于鴻蒙分布式跨設(shè)備文件服務(wù)-信件管理-鴻蒙HarmonyOS技術(shù)社區(qū)

講解到此了,不要忘記了config.json文件的權(quán)限配置哦,在module下添加

  1. "reqPermissions": [ 
  2.     { 
  3.       "name""ohos.permission.DISTRIBUTED_DATASYNC" 
  4.     }, 
  5.     { 
  6.       "name""ohos.permission.GET_DISTRIBUTED_DEVICE_INFO" 
  7.     }, 
  8.     { 
  9.       "name""ohos.permission.DISTRIBUTED_DEVICE_STATE_CHANGE" 
  10.     }, 
  11.     { 
  12.       "name""ohos.permission.WRITE_MEDIA" 
  13.     }, 
  14.     { 
  15.       "name""ohos.permission.READ_MEDIA" 
  16.     } 
  17.   ], 

 想了解更多內(nèi)容,請?jiān)L問:

51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)

https://harmonyos.51cto.com

 

責(zé)任編輯:jianghua 來源: 鴻蒙社區(qū)
相關(guān)推薦

2021-08-13 13:53:23

鴻蒙HarmonyOS應(yīng)用

2020-11-13 12:09:46

HarmonyOS

2023-05-29 14:07:00

Zuul網(wǎng)關(guān)系統(tǒng)

2022-02-17 18:08:04

OpenHarmon應(yīng)用開發(fā)鴻蒙

2014-12-08 10:02:46

Docker開源跨容器服務(wù)

2023-12-29 08:18:31

Session分布式系統(tǒng)微服務(wù)

2015-04-21 09:39:03

javajava分布式爬蟲

2017-10-24 11:28:23

Zookeeper分布式鎖架構(gòu)

2023-11-20 15:32:29

2010-11-01 05:50:46

分布式文件系統(tǒng)

2021-05-28 09:52:00

鴻蒙HarmonyOS應(yīng)用

2011-07-26 09:57:02

分布式網(wǎng)絡(luò)管理帶外網(wǎng)絡(luò)

2021-08-16 09:55:41

鴻蒙HarmonyOS應(yīng)用

2017-10-17 08:33:31

存儲系統(tǒng)分布式

2017-03-14 11:52:52

微服務(wù)架構(gòu)數(shù)據(jù)管理

2022-03-08 15:24:23

BitMapRedis數(shù)據(jù)

2017-04-13 10:51:09

Consul分布式

2020-07-15 09:20:48

MyCatMySQL分布式

2019-10-10 09:16:34

Zookeeper架構(gòu)分布式

2017-09-01 05:35:58

分布式計(jì)算存儲
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號