成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

基于鴻蒙分布式跨設備文件服務-信件管理

開發 前端 分布式
這個跨設備信件管理應用,A手機創建一封信件,生成圖片,在A手機顯示本地端, 在B手機顯示遠程端, 同時A,B手機都可以打開查看信件內容,這里使用到了分布式數據庫管理,使用列表存儲圖片名.

[[431770]]

想了解更多內容,請訪問:

51CTO和華為官方合作共建的鴻蒙技術社區

https://harmonyos.51cto.com

一. 前言

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

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

二. 實現效果

開發工具環境下視頻:https://www.bilibili.com/video/BV16L4y1i7b1/

手機+手機環境下視頻:https://www.bilibili.com/video/BV1mL411g72B/

三. 創建工程

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

基于鴻蒙分布式跨設備文件服務-信件管理-鴻蒙HarmonyOS技術社區

Java模塊布局模塊

四. 主界面開發

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

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.      * 構造方法 
  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.             // 設置圖片參數 
  52.             ImageSource.DecodingOptions decodingOptions = new ImageSource.DecodingOptions(); 
  53.             decodingOptions.desiredSize=new Size(720,1080); 
  54.             PixelMap pixelMap = imageSource.createPixelmap(decodingOptions); 
  55.             //用于保存畫圖結果 
  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.             // 把內容寫到信紙上 
  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, "創建成功!"); 
  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.         // 根據分布式文件路徑,生成文件 
  103.         File file = new File(filePath); 
  104.         if (!file.exists()) { 
  105.             // 如果文件不存在, 調用寫信件 
  106.             writeLetter(fileName, letterContent); 
  107.         } 
  108.         // 圖片參數 
  109.         ImageSource.SourceOptions srcOpts = new ImageSource.SourceOptions(); 
  110.         srcOpts.formatHint = "image/jpeg"
  111.         // 創建圖片源 
  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.     } 

預覽信件內容:

  1. /** 
  2.  * 預覽信件內容 
  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.                 // 設備ID 
  38.                 String deviceID = KvManagerFactory.getInstance().createKvManager(new KvManagerConfig(context)) 
  39.                         .getLocalDeviceInfo().getId(); 
  40.                 // 組合文件名,方便區分是否為當前設備創造的文件 
  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.     } 

 主界面代碼圖:

基于鴻蒙分布式跨設備文件服務-信件管理-鴻蒙HarmonyOS技術社區

講解到此了,不要忘記了config.json文件的權限配置哦,在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.   ], 

 想了解更多內容,請訪問:

51CTO和華為官方合作共建的鴻蒙技術社區

https://harmonyos.51cto.com

 

責任編輯:jianghua 來源: 鴻蒙社區
相關推薦

2021-08-13 13:53:23

鴻蒙HarmonyOS應用

2020-11-13 12:09:46

HarmonyOS

2023-05-29 14:07:00

Zuul網關系統

2025-05-16 08:58:47

Mongodb分布式存儲

2022-02-17 18:08:04

OpenHarmon應用開發鴻蒙

2023-12-29 08:18:31

Session分布式系統微服務

2015-04-21 09:39:03

javajava分布式爬蟲

2017-10-24 11:28:23

Zookeeper分布式鎖架構

2014-12-08 10:02:46

Docker開源跨容器服務

2021-05-28 09:52:00

鴻蒙HarmonyOS應用

2023-11-20 15:32:29

2010-11-01 05:50:46

分布式文件系統

2020-11-06 12:12:35

HarmonyOS

2021-08-16 09:55:41

鴻蒙HarmonyOS應用

2011-07-26 09:57:02

分布式網絡管理帶外網絡

2017-10-17 08:33:31

存儲系統分布式

2020-07-15 09:20:48

MyCatMySQL分布式

2022-03-08 15:24:23

BitMapRedis數據

2017-04-13 10:51:09

Consul分布式

2021-07-22 10:20:21

鴻蒙HarmonyOS應用
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 孰女乱色一区二区三区 | 一区二区三区视频在线免费观看 | 免费网站国产 | www.欧美.com| 天天综合久久 | 一区二区三区在线观看免费视频 | 91精品久久久久久久久 | 国产乱码精品一区二区三区忘忧草 | 亚洲免费三区 | 欧美精品一区二区三区四区五区 | 色桃网 | 国产精品久久 | 亚洲二区视频 | 一级毛片免费 | 91亚洲精品在线 | 日日操夜夜操天天操 | 91精品久久久久久久久中文字幕 | 日韩最新网址 | 日韩不卡一区二区三区 | 久久精品亚洲欧美日韩久久 | 亚洲电影在线播放 | 韩国精品在线观看 | 日本小电影网站 | 久久日韩精品一区二区三区 | 国产精品久久久久aaaa九色 | 91精品国产综合久久久久久 | 欧美一区二区在线播放 | 久久综合激情 | 国产高清精品在线 | 91视频一区二区三区 | 欧美黄色大片在线观看 | 国产精品福利一区二区三区 | 国产精品资源在线观看 | 午夜电影日韩 | 色毛片| 国产精品美女久久久久aⅴ国产馆 | 久久久免费在线观看 | 欧美6一10sex性hd| 91综合网| 国产ts人妖另类 | 人人干人人爽 |