react-native保存图片Android实现方法

/图片的路径格式为远程请求,
例如:'http://xxx:8080/image.jpg'
import { Platform, PermissionsAndroid, NativeModules } from 'react-native';
import DeviceInfo from 'react-native-device-info';

let url = 'xxx';
this.requestWritePermission(url);
    if(Platform.OS === 'android') {
        if(!!url) {
            if (parseFloat(DeviceInfo.getSystemVersion()) >= 6.0) {//6.0及以上
                console.log(DeviceInfo.getSystemVersion());
                this.requestWritePermission(url);
            }else {
                NativeModules.imageDownload.imageProcess(url, (boo) => {
                    console.log(boo);
                    if (boo) {
                        Toast.show("图片保存成功!", 1);
                    } else {
                        Toast.show("图片保存失败!", 1);
                    }
                });
            }
        }
    }
/**
 * 请求Android写权限
 */
async requestWritePermission(url) {

    try {
        const grantedWrite = await PermissionsAndroid.request(
            PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
        )
        if (grantedWrite === PermissionsAndroid.RESULTS.GRANTED) {
            NativeModules.imageDownload.imageProcess(url, (boo) => {
                console.log(boo);
                if(boo) {
                    Toast.info("图片保存成功!", 2);            
                }else {
                    Toast.info("图片保存失败!", 2);
                }
            });
        } else {
            Toast.info("请开启手机存储权限", 2);
        }
    } catch (err) {
        console.warn(err)
    }

}

不用原生现在也可以解决了,需要的加群,
前端交流群:498524034

posted @ 2017-10-16 16:07  FE-神鸟  阅读(1670)  评论(0编辑  收藏  举报