长按保存图片到系统相册

使用

   new QMUIDialog.MessageDialogBuilder(this)
                    .setTitle("保存到相册?")
                    .addAction("取消", (dialog, index) -> {
                        dialog.dismiss();
                    })
                    .addAction(0, "确定", QMUIDialogAction.ACTION_PROP_NEGATIVE, (dialog, index) -> {
                        Flowable.create((FlowableOnSubscribe) e -> {
                            Bitmap bitmap = BitmapUtil.returnBitmap(qrcodeUrl);
                            e.onNext(bitmap);
                            e.onComplete();
                        }, BackpressureStrategy.BUFFER)
                                .subscribeOn(Schedulers.io())
                                .observeOn(AndroidSchedulers.mainThread())
                                .compose(bindToLifecycle())
                                .subscribe(bitmap -> {
                                    BitmapUtil.saveImageToGallery(getApplicationContext(), bitmap, () -> {
                                        Toasty.success(getApplicationContext(), "保存成功").show();
                                    });
                                });

                        dialog.dismiss();
                    })
                    .show();

工具类

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Created by zkt on 2017-12-11.
 */

public class BitmapUtil {
    /**
     * 通过url返回一个bitmap
     *
     * @param url
     * @return
     */
    public final static Bitmap returnBitmap(String url) {
        Bitmap bitmap = null;
        try {
            URL myFileUrl = new URL(url);

            HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
            conn.setDoInput(true);
            conn.connect();
            InputStream is = conn.getInputStream();
            bitmap = BitmapFactory.decodeStream(is);

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return bitmap;
    }

    /**
     * 保存图片到系统相册
     */
    public static void saveImageToGallery(Context context, Bitmap bmp, SaveGalleryListener saveGallery) {
        // 首先保存图片
        File appDir = new File(Environment.getExternalStorageDirectory(), "Boohee");
        if (!appDir.exists()) {
            appDir.mkdir();
        }
        String fileName = System.currentTimeMillis() + ".jpg";
        File file = new File(appDir, fileName);
        try {
            FileOutputStream fos = new FileOutputStream(file);
            bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.flush();
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 其次把文件插入到系统图库
        try {
            MediaStore.Images.Media.insertImage(context.getContentResolver(),
                    file.getAbsolutePath(), fileName, null);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        // 最后通知图库更新
        context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + file.getAbsolutePath())));

        saveGallery.saveComplete();
    }
    

    public interface SaveGalleryListener {
        void saveComplete();
    }
}

参考:http://stormzhang.com/android/2014/07/24/android-save-image-to-gallery/
http://blog.csdn.net/u013042707/article/details/50553701

你可能感兴趣的:(长按保存图片到系统相册)