react-native源码中给android程序员福利!!
来源:互联网 发布:tensorflow读音 编辑:程序博客网 时间:2024/06/05 16:22
前言:最近在搞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大神写的框架!!!!!哈哈哈~~~“
1 0
- react-native源码中给android程序员福利!!
- [Android中react-native调用Native]
- React-Native系列Android源码分析
- React Native源码中JavaScriptCore详解
- React Native源码中JavaScriptCore详解
- React-Native-源码分析
- React Native源码学习
- React Native源码编译
- React Native 源码浅析
- React Native嵌入Android原生项目中
- Android项目中集成React Native
- Android项目中集成React Native
- android中react-native热更新
- android原有项目中集成react-native
- Android项目中集成React Native
- react-native中给图片设置原角
- React Native Android入门实战及深入源码分析系列(2)——React Native源码编译
- react native for android
- poj 1031
- 一个XSLT的变量、参数和模板调用的问题
- pat-bl-1046
- 重温《JAVA编程思想》----2017.1.21动态代理
- web安全相关资料
- react-native源码中给android程序员福利!!
- 一起学WEB(三) CSS到底是干啥的
- (c++)输入三个字符串,按由小到大的顺序输出
- Socket的监听的三种方式
- javascript
- 支付宝 Android 版使用的开源组件
- SpringAOP快速使用
- hrbust 哈理工 1013
- PHP中@的用法