안드로이드 다운로드 매니저, 스캐너

설명
다운로드 폴더에서도 확인할 수 있음
Tags
download
file
External Storage
android
 

안드로이드 외부저장소 연동

앱 샌드박스 내에서가 아닌, External Storage에 저장함으로써 사진, 다운로드 등 built-in apps에서도 볼 수 있도록 하기 위해서는 Media Scanner 또는 Download Manager를 활용해야 한다.

[1] Media Scanner

  • 미디어 스캐너는 파일을 검색하고 지정된 MIME type별로 분류합니다.
  • MIME type을 지정하지 않은 경우 파일 확장자로 분류합니다.
RNFetchBlob .config({ // DCIMDir is in external storage path : dirs.DCIMDir + '/music.mp3' }) .fetch('GET', 'http://example.com/music.mp3') .then((res) => RNFetchBlob.fs.scanFile([ { path : res.path(), mime : 'audio/mpeg' } ])) .then(() => { // scan file success }) .catch((err) => { // scan file error })

[2] Download Manager

크기가 큰 파일을 다운로드 받을 때에는 Download Manager를 이용하는 것이 추천됨.
프로그레스 바, 푸시 알림, 다운로드 태스트 관리 등 다양한 네이티브 기능 지원됨
notion image
  • DownloadManager는 파일을 외부 스토리지에만 저장할 수 있다
    • 따라서 fileCachepath 프롭은 무시된다.
  • DownloadManager는 GET 메소드만 지원되므로, request body는 무시된다.
  • 다운로드 완료 후, path()를 통해 다운로드 경로를 받아서 쓸 수 있다.
export const androidDownloadManager = (fileLink: string) => { RNFetchBlob.config({ // android only options, these options be a no-op on IOS addAndroidDownloads: { useDownloadManager: true, notification: true, // 다운로드 완료 푸시알림 표시 여부 path: `${RNFetchBlob.fs.dirs.DownloadDir}/test.jpg`, // 아래는 옵셔널이지만, 안적으면 확장자가 없을 때에는 에러나므로 적어주는 것을 추천함. mime: 'image/jpg', description: 'Your test reports.', }, }) .fetch('GET', fileLink) .then(resp => { // the path of downloaded file resp.path(); }); };
Your app might not have right to remove/change the file created by Download Manager, therefore you might need to set custom location to the download task.
Download Notification and Visibility in Download App (Android Only)
notion image
notion image
If you need to display a notification upon the file is downloaded to storage (as the above) or make the downloaded file visible in "Downloads" app. You have to add some options to config
RNFetchBlob.config({ fileCache : true, // android only options, these options be a no-op on IOS addAndroidDownloads : { // Show notification when response data transmitted notification : true, // Title of download notification title : 'Great ! Download Success ! :O ', // File description (not notification description) description : 'An image file.', mime : 'image/png', // Make the file scannable by media scanner mediaScannable : true, } }) .fetch('GET', 'http://example.com/image1.png') .then(...)
Open Downloaded File with Intent
This is a new feature added in 0.9.0 if you're going to open a file path using official Linking API that might not work as expected, also, if you're going to install an APK in Downloads app, that will not function too. As an alternative, you can try actionViewIntent API, which will send an ACTION_VIEW intent for you which uses the given MIME type.
Download and install an APK programmatically
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')