Skip to content

Android API

Ron Radtke edited this page Feb 5, 2022 · 3 revisions

RNFetchBlob.android

android.actionViewIntent(path, mime):Promise

0.9.0

When sending an ACTION_VIEW intent with given file path and MIME type, system will try to open an App to handle the file. For example, open Gallery app to view an image, or install APK.

path:string

Path of the file to be opened.

mime:string

Basically system will open an app according to this MIME type.

For example, download and install an APK programatically

const android = RNFetchBlob.android

RNFetchBlob.config({
    addAndroidDownloads : {
      useDownloadManager : true,
      title : 'awesome.apk',
      description : 'An APK that will be installed',
      mime : 'application/vnd.android.package-archive',
      mediaScannable : true,
      notification : true,
    }
  })
  .fetch('GET', `http://www.example.com/awesome.apk`)
  .then((res) => {
      android.actionViewIntent(res.path(), 'application/vnd.android.package-archive')
  })

Or show an image in image viewer

      android.actionViewIntent(PATH_OF_IMG, 'image/png')

android.getContentIntent(mime:string):Promise

0.10.5

mime:?string (Optional)

MIME type filter, only the files matches the MIME will be shown.

This method brings up OS default file picker and resolves a file URI when the user selected a file. However, it does not resolve or reject when user dismiss the file picker via pressing hardware back button, but you can still handle this behavior via AppState

Here's an example

  let handler = (state) =>{
    if(state === 'active') {
      console.log('did not select any file, but all good.')
      AppState.removeEventListener('change', handler)
    }
  }
  
  AppState.addEventListener('change', handler)

  RNFetchBlob.android.getContentIntent('image/png').then((files) => {
    console.log(files)
  })

android.addCompleteDownload(options:AndroidDownloadOption):Promise

0.10.6

Using this function to add an existing file to Downloads app.

An object that for setting the title, description, mime, and notification of the item.

  RNFetchBlob.android.addCompleteDownload({
    title : 'test file of RNFB',
    description : 'desc',
    mime : 'image/png',
    path : SomePath,
    showNotification : true
  })
Clone this wiki locally