react-native 保存圖片到相冊(cè)

一,iOS 保存圖片

1,添加依賴庫(kù)

WechatIMG27.png
WechatIMG28.png
WechatIMG29.png

2. react-native中使用

import { CameraRoll } from 'react-native';
var promise = CameraRoll.saveToCameraRoll('網(wǎng)絡(luò)圖片')
                                                promise.then(function (result) {
                                                    //Toast.info('圖片已保存至相冊(cè)');

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

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

一,android 保存圖片

前言:網(wǎng)上查了很多資料有讓那個(gè)用react-native-fs庫(kù)的。我是卡在了下載gradle-core1.5.1.jar包上,一直下載不下來(lái),所以采用了另外一種方式:直接調(diào)用原生的android下載功能

1,源代碼

  // 加權(quán)限
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

這塊省略創(chuàng)建原聲橋接文件

  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);
                //通過(guò)io流的方式來(lái)壓縮保存圖片
                boolean isSuccess = bmp.compress(Bitmap.CompressFormat.JPEG, 60, fos);
                fos.flush();
                fos.close();

                //把文件插入到系統(tǒng)圖庫(kù)
                //MediaStore.Images.Media.insertImage(context.getContentResolver(), file.getAbsolutePath(), fileName, null);

                //保存圖片后發(fā)送廣播通知更新數(shù)據(jù)庫(kù)
                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();// 關(guān)閉流
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return bm;
    }

原聲調(diào)用:

import { NativeModules } from 'react-native';
NativeModules.updateApp.saveImageToGallery(‘網(wǎng)絡(luò)圖片地址’);
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容