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. First project in android
  2. Android(安卓)下拉刷新 ListRefresh 从网络加载图片
  3. android 保存和读取文件
  4. android 之Dialog对话框(简易版)
  5. android UI组件
  6. android手机端保存xml数据
  7. Android常用权限
  8. android 双击图片放大缩小
  9. android加载本地图片

随机推荐

  1. API 23及之后版本关于用android studio
  2. android 连接CMWAP
  3. android导出通讯录,通话记录,短信
  4. android rating bar style
  5. Android开发学习笔记:我的第一个Android程
  6. android 关于再按一次退出程序效果
  7. 【Android】Http请求
  8. Android 超级水平仪 金属红色仪表风格发
  9. Android中的GridView图片异步加载
  10. js判断移动端系统