保存到图库的图像仅在手机重新启动时显示 - React-Native Android

Lar*_*ney 1 image android-gallery react-native react-native-fs

我正在使用react-native-fetch-blob库中的文件系统API

我从服务器请求图像,接收 Base64,然后将其保存到 android fs 中的 Pictures 目录。

我这样保存图像:

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!!")

    }).catch((error) => {
    // error handling
    console.log("Error:", error)
  });
},
Run Code Online (Sandbox Code Playgroud)

问题
图像保存成功,但当我打开 Android 的图库时,它不可见。但是当我关闭并打开 Android 模拟器时,它就会在图库中可见。

问题
如何在不重启模拟器的情况下查看Android图库中的图片?


解决方案


创建文件后,我将其添加到上述方法中:

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

谢谢你@Xeijp。

小智 5

在 Android 中,要使图像在图库中可见,您需要对该文件使用“媒体扫描仪”。react-native-fetch-blob 确实为此提供了一个 API

fs.scanFile(path_of_the_file)
Run Code Online (Sandbox Code Playgroud)

文档