1.保存图片工具类

package com.changle.app.GoodManners.MyDialog;import android.content.Context;import android.content.Intent;import android.graphics.Bitmap;import android.net.Uri;import android.os.Environment;import android.provider.MediaStore;import com.changle.app.util.ToastUtil;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;/** * Created by Jongey on 2019/11/22. */public class SaveImageUtils {    public static void saveImageToGallery(Context context, Bitmap bmp) {        // 首先保存图片        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.getPath())));    }    public static void saveImageToGallerys(Context context, Bitmap bmp) {        if (bmp == null){            ToastUtil.showShortMessage(context, "保存出错了...");            return;        }        // 首先保存图片//        File appDir = new File(BaseApplication.app.getTmpDir(), "ywq");        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) {            ToastUtil.showShortMessage(context, "文件未发现");            e.printStackTrace();        } catch (IOException e) {            ToastUtil.showShortMessage(context, "保存出错了...");            e.printStackTrace();        }catch (Exception e){            ToastUtil.showShortMessage(context, "保存出错了...");            e.printStackTrace();        }        // 最后通知图库更新        try {            MediaStore.Images.Media.insertImage(context.getContentResolver(), file.getAbsolutePath(), fileName, null);        } catch (FileNotFoundException e) {            e.printStackTrace();        }        Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);        Uri uri = Uri.fromFile(file);        intent.setData(uri);        context.sendBroadcast(intent);        ToastUtil.showShortMessage(context, "保存成功了...");    }}

2.调用方法保存图片(可设置长按或点击事件)//此处注意对手机读写权限,如果未开启权限需要开启权限,否则会保存失败

//读写权限private static String[] PERMISSIONS_STORAGE = {        android.Manifest.permission.READ_EXTERNAL_STORAGE,        android.Manifest.permission.WRITE_EXTERNAL_STORAGE};    //请求状态码private static int REQUEST_PERMISSION_CODE = 2;if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {    if (ActivityCompat.checkSelfPermission(me, Manifest.permission.WRITE_EXTERNAL_STORAGE)            != PackageManager.PERMISSION_GRANTED) {        // 没有写的权限,去申请写的权限,会弹出对话框        ActivityCompat.requestPermissions(ShowPhotoActivity.this, PERMISSIONS_STORAGE, REQUEST_PERMISSION_CODE);    }}StrictMode.ThreadPolicy policy=new StrictMode.ThreadPolicy.Builder().permitAll().build();StrictMode.setThreadPolicy(policy);
setRightOnClickListener(new View.OnClickListener() {    @Override    public void onClick(View v) {            if(url!=null){                new Thread(new Runnable() {                    @Override                    public void run() {                        try {                            final Bitmap myBitmap = Glide.with(ShowPhotoActivity.this)//上下文                                    .load(url)//url                                    .asBitmap() //必须                                    .centerCrop()                                    .into(500, 500)                                    .get() ;                            Log.d("sevaImage", "onClick: "+myBitmap.getByteCount());                            runOnUiThread(new Runnable() {                                @Override                                public void run() {                                    SaveImageUtils.saveImageToGallerys(ShowPhotoActivity.this,myBitmap);                                }                            });                        } catch (Exception e) {                            e.printStackTrace();                        }                    }                }).start();            }    }});

更多相关文章

  1. Android 下拉刷新 ListRefresh 从网络加载图片
  2. android 双击图片放大缩小
  3. android加载本地图片
  4. Android 从网络加载图片
  5. Android从相机或相册获取图片裁剪
  6. android:动态获取权限
  7. android camera2 api点击图片实现聚焦

随机推荐

  1. Android 2.1 源码结构分析 转载
  2. Android植物大战僵尸附源码
  3. GitHub上受欢迎的Android UI Library
  4. SharedPreferences之Android数据保存
  5. 通过userAgent判断是是否手机、微信、手
  6. Android第五个功能:文件存储到SDCard上面
  7. 原生APP中js怎样与Android和ios进行交互
  8. Android 复盘——你真的了解 setContentV
  9. Android(14) ArrayAdapter(数组适配器)的三
  10. android 出错信息为:Class 'Anonymous cla