react-native 保存图片到相册

一,iOS 保存图片

1,添加依赖库

WechatIMG27.png
WechatIMG28.png
WechatIMG29.png

2. react-native中使用

import { CameraRoll } from 'react-native';
var promise = CameraRoll.saveToCameraRoll('网络图片')
                                                promise.then(function (result) {
                                                    //Toast.info('图片已保存至相册');

                                                }).catch(function (error) {
                                                    //Toast.info('保存失败');
                                                })

以上iOS端保存图片功能已完成

一,android 保存图片

前言:网上查了很多资料有让那个用react-native-fs库的。我是卡在了下载gradle-core1.5.1.jar包上,一直下载不下来,所以采用了另外一种方式:直接调用原生的android下载功能

1,源代码

  // 加权限
 

这块省略创建原声桥接文件

  private static String[] PERMISSIONS_STORAGE = {
                        Manifest.permission.READ_EXTERNAL_STORAGE,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE };

@ReactMethod
    public boolean saveImageToGallery(String url) {
        Bitmap bmp = getBitmap(url);
        Context context = getCurrentActivity();
        int permission = ActivityCompat.checkSelfPermission(getCurrentActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE);
        if (permission != PackageManager.PERMISSION_GRANTED) {
            // We don't have permission so prompt the user
            ActivityCompat.requestPermissions(getCurrentActivity(), PERMISSIONS_STORAGE,
                                         1);
        } else {
            // 首先保存图片
            String storePath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "dearxy";
            File appDir = new File(storePath);
            if (!appDir.exists()) {
                appDir.mkdirs();
            }
            String fileName = System.currentTimeMillis() + ".jpg";
            File file = new File(appDir, fileName);
            try {
                FileOutputStream fos = new FileOutputStream(file);
                //通过io流的方式来压缩保存图片
                boolean isSuccess = bmp.compress(Bitmap.CompressFormat.JPEG, 60, fos);
                fos.flush();
                fos.close();

                //把文件插入到系统图库
                //MediaStore.Images.Media.insertImage(context.getContentResolver(), file.getAbsolutePath(), fileName, null);

                //保存图片后发送广播通知更新数据库
                Uri uri = Uri.fromFile(file);
                context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri));
                if (isSuccess) {
                    Toast.makeText(getCurrentActivity(), "成功保存图片", Toast.LENGTH_SHORT).show();
                    return true;
                } else {
                    Toast.makeText(getCurrentActivity(), "保存图片失败", Toast.LENGTH_SHORT).show();
                    return false;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    public Bitmap getBitmap(String url) {
        Bitmap bm = null;
        try {
            URL iconUrl = new URL(url);
            URLConnection conn = iconUrl.openConnection();
            HttpURLConnection http = (HttpURLConnection) conn;

            int length = http.getContentLength();

            conn.connect();
            // 获得图像的字符流
            InputStream is = conn.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is, length);
            bm = BitmapFactory.decodeStream(bis);
            bis.close();
            is.close();// 关闭流
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return bm;
    }

原声调用:

import { NativeModules } from 'react-native';
NativeModules.updateApp.saveImageToGallery(‘网络图片地址’);

你可能感兴趣的:(react-native 保存图片到相册)