保存图片到手机相册的代码

  File appDir = new File(Environment.getExternalStorageDirectory(), "Boohee");
                if (!appDir.exists()) {
                    appDir.mkdir();
                }
                String fileName = System.currentTimeMillis() + ".jpg";
                File file = new File(appDir, fileName);
                try {
                    FileOutputStream fos = new FileOutputStream(file);
                   bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
                    fos.flush();
                    fos.close();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

                // 其次把文件插入到系统图库
                try {
                    MediaStore.Images.Media.insertImage(this.getContentResolver(),
                            file.getAbsolutePath(), fileName, null);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
                // 最后通知图库更新
               this.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse(file.getAbsolutePath())));

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue.js是一个开源的前端JavaScript框架,用于构建现代化的Web应用程序。在Vue.js中,保存图片手机相册需要使用HTML5中的File API和Cordova插件。具体步骤如下: 1. 安装Cordova插件 使用Vue CLI创建Vue.js项目后,需要安装cordova插件,运行以下命令: ``` npm install --save cordova-plugin-file cordova-plugin-file-transfer cordova-plugin-camera ``` 2. 加载Cordova插件 安装好插件后,在Vue.js中加载cordova插件。在main.js中添加以下代码: ```javascript document.addEventListener("deviceready", onDeviceReady, false); function onDeviceReady() { console.log("Device ready, Cordova plugins loaded."); } ``` 3. 获取图片文件 使用HTML5中的File API选择用户想要保存的图片文件,并将其转换为base64编码。以下是代码示例: ```javascript // 获取图片文件 var file = this.$refs.imageInput.files[0]; // 将文件转换为base64编码 var reader = new FileReader(); reader.readAsDataURL(file); reader.onloadend = function () { var base64 = reader.result; } ``` 4. 保存图片相册 使用Cordova插件将base64编码的图片保存到手机相册中。以下是代码示例: ```javascript // 保存图片相册 var filePath = "file:///storage/emulated/0/Download/image.jpg"; var fileTransfer = new FileTransfer(); fileTransfer.download( base64, filePath, function(entry) { console.log("Image saved: " + entry.toURL()); alert("Image saved successfully!"); }, function(error) { console.log("Image save error: " + error.code + ":" + error.source + ":" + error.target); alert("Image save error: " + error.code); } ); ``` 在以上代码示例中,base64是图像文件,filePath是要保存到的路径,fileTransfer.download()方法将base64编码的图像文件下载到filePath指定的位置,保存到手机相册中。 综上所述,以上是Vue.js保存图片手机相册的基本步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值