uniapp 安卓文件分享功能

通过插件lime-share调用系统分享功能,如果是文件直接分享,如果是文件夹调用对文件夹进行压缩后分享。

// 引入jszip
import {shareWithSystem} from '@/uni_modules/lime-share'
import { deleteFile } from "@/utils/io";

/**
 * 
 * @param {*} sharePath 相对路径
 * @param {*} type file 文件,dir 目录
 * @returns 
 */
export default function fileShare(sharePath, type = "file") {
  return  new Promise(async (resolve, reject) => {
    let basePath = plus.io.convertLocalFileSystemURL(`_doc`);
    sharePath = `${basePath}/${sharePath}`;    
    // 如果是文件,则直接压缩分享
    if(type === "file") {
      shareWithSystem({
        path: sharePath,
        type: "file",
        title: "分享到",
        fail: () => {
          reject("文件分享失败");
        },
        success: () => {
          resolve();
        }
      })
    }else {
      // 如果是文件夹,则压缩整个文件夹
      deleteFile("shareZip").finally(() => {
        const zipName = sharePath.split("/").pop();
        plus.zip.compress(sharePath, `${basePath}/shareZip/${zipName}.zip`, () => {
          shareWithSystem({
            path: `${basePath}/shareZip/${zipName}.zip`,
            type: "file",
            title: "分享到",
            success: () => {
              resolve();
            },
            fail: (e) => {
              reject("文件分享失败" + e.message);
            }
          })
        }, (e) => {
          if(e.message.includes("no such file or directory")) {
            reject("文件不存在");
          }else {
            reject("文件压缩失败");
          }
        });
      })
    }
  })
}

 

你可能感兴趣的:(uni-app,android)