React-native-android - 如何将图像保存到Android文件系统并在手机的“图库”中查看 [英] React-native-android - How to save an image to the Android file system and view in the phone's 'Gallery'

本文介绍了React-native-android - 如何将图像保存到Android文件系统并在手机的“图库”中查看的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将图像保存到android的本地文件系统,以便从手机的图库和文件夹中查看图像?



我发现这个 react-native-fs 库,但在研究文档并通过一个例子工作之后,我仍然不确定是否有可能。

谢谢

解决方案

对于有同样问题的人,解决方案。

解决方案



我使用文件系统API react-native-fetch-blob 库。这是因为我认为它比react-native-fs库有更好的文档记录和更容易理解。



我从服务器请求一个图像, base64,然后将它保存到android fs中的Pictures目录下。
$ b

我保存这样的图像:

  var RNFetchBlob = require('react-native-fetch-blob')。 

const PictureDir = RNFetchBlob.fs.dirs.PictureDir;

getImageAttachment:功能(uri_attachment,filename_attachment,mimetype_attachment){

返回新无极((RESOLVE,REJECT)=> {

//获取附件
RNFetchBlob.fetch('GET',config.apiRoot +'/ app /'+ uri_attachment)
.then((response)=> {

let base64Str = response.data;

let imageLocation = PictureDir +'/'+ filename_attachment;
$ b //保存图片
fs.writeFile(imageLocation,base64Str,'base64') ;
console.log(FILE CREATED !!)

RNFetchBlob.fs.scanFile([{path:imageLocation,mime:mimetype_attachment}])
.then(( )=> {
console.log(scan file success)
})
.catch((err)=> {
console.log(scan file错误)
})

})。catch((error)=> {
//错误处理
console.log(Error:,错误)
});
},

在上面的方法中的代码刷新画廊,否则图像不会显示,直到手机关闭,然后再次打开。

  RNFetchBlob.fs.scanFile([{path:imageLocation,mime:mimetype_attachment}])
.then(()=> {
console.log(scan file success)
})
.catch((err)=> {
console.log(scan file error)
$)

享受!

Is it possible to save an image to the android's local file system so it can be viewed from the phone's 'Gallery' and in a folder??

I found this react-native-fs library but after studying the documentation and working through an example I am still unsure if it is possible.

Thanks

解决方案

For anyone having the same problem, here is the solution.

Solution

I am using the File System API from the react-native-fetch-blob library. This is because I tought it was way better documented and easier to understand than the 'react-native-fs' library.

I request an image from the server, receive a base64 and I then save it to the Pictures directory in the android fs.

I save the image like this:

var RNFetchBlob = require('react-native-fetch-blob').default;

const PictureDir = RNFetchBlob.fs.dirs.PictureDir;

getImageAttachment: function(uri_attachment, filename_attachment, mimetype_attachment) {

   return new Promise((RESOLVE, REJECT) => {

   // Fetch attachment
   RNFetchBlob.fetch('GET', config.apiRoot+'/app/'+uri_attachment)
   .then((response) => {

     let base64Str = response.data;

     let imageLocation = PictureDir+'/'+filename_attachment;

     //Save image
     fs.writeFile(imageLocation, base64Str, 'base64');
     console.log("FILE CREATED!!")

     RNFetchBlob.fs.scanFile([ { path : imageLocation, mime : mimetype_attachment } ])
     .then(() => {
       console.log("scan file success")
     })
     .catch((err) => {
       console.log("scan file error")
     })

    }).catch((error) => {
    // error handling
    console.log("Error:", error)
  });
},

The following code that is in the above method refreshes the Gallery otherwise the images would not display untill the phone is turned off and back on again.

RNFetchBlob.fs.scanFile([ { path : imageLocation, mime : mimetype_attachment } ])
.then(() => {
  console.log("scan file success")
})
.catch((err) => {
  console.log("scan file error")
})

Enjoy!

这篇关于React-native-android - 如何将图像保存到Android文件系统并在手机的“图库”中查看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆