uniapp原生插件之安卓文件操作原生插件

插件介绍

安卓文件操作原生插件,读写文件,文件下载等,支持读取移动设备路径等外部存储设备路径,如U盘路径

插件地址

安卓文件操作原生插件 - DCloud 插件市场

超级福利

uniapp 插件购买超级福利

详细使用文档

uniapp 安卓文件操作原生插件使用文档

插件所需权限

  • android.permission.INTERNET
  • android.permission.WRITE_EXTERNAL_STORAGE
  • android.permission.READ_EXTERNAL_STORAGE

用法

在需要使用插件的页面加载以下代码

javascript 复制代码
const module = uni.requireNativePlugin("leven-file-FileModule");

页面内容

html 复制代码
<template>
  <view>
    <uni-card title="文件类">
      <button type="primary" @click="getFileFromSdcard">从sd卡取文件</button>
      <button type="primary" @click="saveFileToSdcard">保存文件到sd</button>
      <button type="primary" @click="getFileSizes">获取文件大小</button>
      <button type="primary" @click="getDirSize">递归取得文件夹大小</button>
      <button type="primary" @click="formatFileSize">格式化文件大小</button>
      <button type="primary" @click="getFileListCount">递归求取目录文件个数</button>
      <button type="primary" @click="searchSdCardExtFile">根据扩展名搜索sdcard文件</button>
      <button type="primary" @click="searchKeywordFile">根据关键字搜索sdcard文件</button>
      <button type="primary" @click="getDirAllFiles">获取目录下所有的文件</button>
      <button type="primary" @click="copyFile">复制文件</button>
      <button type="primary" @click="deleteFile">删除文件或目录</button>
      <button type="primary" @click="writeFile">写文件</button>
      <button type="primary" @click="getPathFileName">获取路径的文件名</button>
      <button type="primary" @click="readFileLine">读取文件(按行读取)</button>
      <button type="primary" @click="readFile">读取整个文件</button>
      <button type="primary" @click="downloadFile">下载文件</button>
    </uni-card>
  </view>
</template>

<script>
  import {
    requestAndroidPermission
  } from "@/utils/permission.js"
  const module = uni.requireNativePlugin("leven-file-FileModule");
  export default {
    data() {
      return {}
    },
    mounted() {
      // 动态开启应用权限
      this.openAndroidPermission();
    },
    methods: {
      // 从sd卡取文件
      getFileFromSdcard() {
        module.getFileFromSdcard({
          filename: "leven_file/a/1.txt"
        }, res => {
          console.log(res)
        })
      },
      // 保存文件到sd
      saveFileToSdcard() {
        module.saveFileToSdcard({
          filename: "leven_file/a/save1.txt",
          content: "这是一条测试内容"
        }, res => {
          console.log(res)
        })
      },
      // 获取文件大小
      getFileSizes() {
        module.getFileSizes({
          filePath: "/storage/emulated/0/leven_file/a/1.txt",
        }, res => {
          console.log(res)
        })
      },
      // 递归取得文件夹大小
      getDirSize() {
        module.getDirSize({
          dirPath: "/storage/emulated/0/qqmusic",
        }, res => {
          console.log(res)
        })
      },
      // 格式化文件大小
      formatFileSize() {
        module.formatFileSize({
          size: 3456789,
        }, res => {
          console.log(res)
        })
      },
      // 递归求取目录文件个数
      getFileListCount() {
        module.getFileListCount({
          dirPath: "/storage/emulated/0/qqmusic",
        }, res => {
          console.log(res)
        })
      },
      // 根据扩展名搜索sdcard文件
      searchSdCardExtFile() {
        module.searchSdCardExtFile({
          dirPath: "/storage/emulated/0/qqmusic",
          ext: ['png', 'txt'],
        }, res => {
          console.log(res)
        })
      },
      // 根据关键字搜索sdcard文件
      searchKeywordFile() {
        module.searchKeywordFile({
          dirPath: "/storage/emulated/0/qqmusic",
          keyword: "1",
        }, res => {
          console.log(res)
        })
      },
      // 获取目录下所有的文件
      getDirAllFiles() {
        module.getDirAllFiles({
          dirPath: "/storage/emulated/0/qqmusic"
        }, res => {
          console.log(res)
        })
      },
      // 复制文件
      copyFile() {
        module.copyFile({
          fromFilename: "/storage/emulated/0/qqmusic/easter_egg_config_res/easter_egg_51.png",
          toFilename: "/storage/emulated/0/leven_file/b/egg_51.png"
        }, res => {
          console.log(res)
        })
      },
      // 删除文件或目录
      deleteFile() {
        module.deleteFile({
          filePath: "/storage/emulated/0/leven_file/a/egg_51.png"
        }, res => {
          console.log(res)
        })
      },
      // 写文件
      writeFile() {
        module.writeFile({
          filename: "/storage/emulated/0/leven_file/a/test_write.txt",
          content: "123456\n",
          append: true
        }, res => {
          console.log(res)
        })
      },
      // 获取路径的文件名
      getPathFileName() {
        module.getPathFileName({
          filePath: "/storage/emulated/0/leven_file/a/test_write.txt"
        }, res => {
          console.log(res)
        })
      },
      // 读取文件(按行读取)
      readFileLine() {
        module.readFileLine({
          filename: "/storage/emulated/0/leven_file/a/test_write.txt",
          startLine: 1,
          lineCount: 1
        }, res => {
          console.log(res)
        })
      },
      // 读取整个文件
      readFile() {
        module.readFile({
          filename: "/storage/emulated/0/leven_file/a/test_write.txt",
          removeWrap: false
        }, res => {
          console.log(res)
        })
      },
      // 下载文件
      downloadFile() {
        module.downloadFile({
          url: "http://www.yeyuboke.com/svga/1.svga",
          saveDir: "/storage/emulated/0/leven_file/"
        }, res => {
          console.log(res)
        })
      },
      showToast(content) {
        uni.showToast({
          title: content,
          icon: "none"
        })
      },
      // 开启应用权限
      openAndroidPermission() {
        let that = this;
        requestAndroidPermission("android.permission.READ_EXTERNAL_STORAGE").then(res => {
          that.showToast(res)
        })
        requestAndroidPermission("android.permission.WRITE_EXTERNAL_STORAGE").then(res => {
          that.showToast(res)
        })
      }
    }
  }
</script>

<style>

</style>

插件方法

  1. 从sd卡读取文件
  2. 保存文件到sd卡
  3. 获取文件大小
  4. 递归取得文件夹大小
  5. 格式化文件大小
  6. 递归求取目录文件个数
  7. 根据扩展名搜索sdcard文件
  8. 根据关键字搜索sdcard文件
  9. 获取目录下所有的文件
  10. 复制文件
  11. 删除文件或目录
  12. 写文件
  13. 获取路径的文件名
  14. 读取文件(按行读取)
  15. 读取整个文件
  16. 远程下载文件到本地
  17. 获取U盘路径
  18. 移动文件
  19. 获取重定向真实url
  20. 批量下载文件(可下载重定向地址)

具体方法的使用可参考详细使用文档

联系作者

购买插件前请先试用,试用通过再购买。在试用中如果遇到任何问题,可与作者联系,QQ:334106817,将全力协助你使用本插件

预览图片

相关推荐
excel1 小时前
Three.js 材质(Material)详解 —— 区别、原理、场景与示例
前端
掘金安东尼1 小时前
抛弃自定义模态框:原生Dialog的实力
前端·javascript·github
用户2018792831675 小时前
Android黑夜白天模式切换原理分析
android
hj5914_前端新手5 小时前
javascript基础- 函数中 this 指向、call、apply、bind
前端·javascript
薛定谔的算法5 小时前
低代码编辑器项目设计与实现:以JSON为核心的数据驱动架构
前端·react.js·前端框架
Hilaku5 小时前
都2025年了,我们还有必要为了兼容性,去写那么多polyfill吗?
前端·javascript·css
yangcode5 小时前
iOS 苹果内购 Storekit 2
前端
LuckySusu5 小时前
【js篇】JavaScript 原型修改 vs 重写:深入理解 constructor的指向问题
前端·javascript
LuckySusu5 小时前
【js篇】如何准确获取对象自身的属性?hasOwnProperty深度解析
前端·javascript
芦半山5 小时前
「幽灵调用」背后的真相:一个隐藏多年的Android原生Bug
android