Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React-native, how to get file-asset image absolute path?

I'm doing some image manipulation on ios on react-native.

The problem is one of the libraries I'm using only supports absolute paths, but I only have the file-asset uri.

Example

I have:

assets-library://asset/asset.HEIC?id=CE542E92-B1FF-42DC-BD89-D61BB70EB4BF&ext=HEIC

I need:

file:///Users/USERNAME/Library/Developer/CoreSimulator/Devices/########-####-####-####-############/data/Containers/Data/Application/########-####-####-####-############/Documents/########-####-####-####-############.jpg

Is there any way to easily get the image absolute path?

like image 737
Oscar Franco Avatar asked Jun 18 '18 12:06

Oscar Franco


1 Answers

This is what I ended up doing, based on @ospfranco's answer. I saved a copy of the asset on the temp folder. Also included a little snippet to generate a random string for the file name.

import RNFS from 'react-native-fs';

getAssetFileAbsolutePath = async (assetPath) => {
    const dest = `${RNFS.TemporaryDirectoryPath}${Math.random().toString(36).substring(7)}.jpg`;

    try {
      let absolutePath = await RNFS.copyAssetsFileIOS(assetPath, dest, 0, 0);
      console.log(absolutePath)
    } catch(err) {
      console.log(err)
    } 
  }
like image 121
Brunno Vodola Martins Avatar answered Nov 13 '22 04:11

Brunno Vodola Martins