react-native源码中给android程序员福利!!

前言:最近在搞react-native,搞过rn的都知道,rn-android其实也就是用js代码去调原生代码的,所以作为一名android程序员是吧,总得看看facebook那些大神写的那些源代码,果然很高端!!!!!

是这样子的,因为我们app原生的在做,rn也在做,app有一个保存app中的图片到本地相册的一个功能,在rn中,很方便啊,直接用rn组件CameraRoll就ok了,但是原生app中网上查了很多资料,保存还是有点问题啊,于是想看一下rn对应的android底层是咋实现的,看了一下,真的是大神写的代码啊,短短几行代码就完美的实现的我的需求!!!!嘿嘿,

我们看到这么一个目录:
这里写图片描述

我们打开代码瞧一瞧:

File source = new File(mUri.getPath());
      FileChannel input = null, output = null;
      try {
        File exportDir = (mType == MediaType.PHOTO)
          ? Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
          : Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES);
        exportDir.mkdirs();
        if (!exportDir.isDirectory()) {
          mPromise.reject(ERROR_UNABLE_TO_LOAD, "External media storage directory not available");
          return;
        }
        File dest = new File(exportDir, source.getName());
        int n = 0;
        String fullSourceName = source.getName();
        String sourceName, sourceExt;
        if (fullSourceName.indexOf('.') >= 0) {
          sourceName = fullSourceName.substring(0, fullSourceName.lastIndexOf('.'));
          sourceExt = fullSourceName.substring(fullSourceName.lastIndexOf('.'));
        } else {
          sourceName = fullSourceName;
          sourceExt = "";
        }
        while (!dest.createNewFile()) {
          dest = new File(exportDir, sourceName + "_" + (n++) + sourceExt);
        }
        input = new FileInputStream(source).getChannel();
        output = new FileOutputStream(dest).getChannel();
        output.transferFrom(input, 0, input.size());
        input.close();
        output.close();

        MediaScannerConnection.scanFile(
            mContext,
            new String[]{dest.getAbsolutePath()},
            null,
            new MediaScannerConnection.OnScanCompletedListener() {
              @Override
              public void onScanCompleted(String path, Uri uri) {
                if (uri != null) {
                  mPromise.resolve(uri.toString());
                } else {
                  mPromise.reject(ERROR_UNABLE_TO_SAVE, "Could not add image to gallery");
                }
              }
            });
      } catch (IOException e) {
        mPromise.reject(e);
      } finally {
        if (input != null && input.isOpen()) {
          try {
            input.close();
          } catch (IOException e) {
            FLog.e(ReactConstants.TAG, "Could not close input channel", e);
          }
        }
        if (output != null && output.isOpen()) {
          try {
            output.close();
          } catch (IOException e) {
            FLog.e(ReactConstants.TAG, "Could not close output channel", e);
          }
        }
      }
    }

搞android的盆友应该都看得懂,拿到图片path,然后保存到相册,然后更新相册内容,全部代码没有一点累赘啊,很爽,

当然,里面封装了肯定不止这么一个东西,我们接着往下看:
这里写图片描述

还有一下运行时权限的工具类啊,自定义的imageview(支持网络加载,圆角,圆形)….. 一些小伙伴整天都说什么“你项目中用的是啥框架啊,可不可以给我参考一下啊,好吧!我想说,去研究下rn吧,facebook大神写的框架!!!!!哈哈哈~~~“

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值