美文网首页
2019-06-29

2019-06-29

作者: 怪咖小青年_56e8 | 来源:发表于2019-06-29 00:14 被阅读0次

    图片保存到相册

    只支持bitmap,因此将imageview转为bitmap

    final ImageView zx_img = (ImageView) view.findViewById(R.id.zx_img);
    final Bitmap image = ((BitmapDrawable)zx_img.getDrawable()).getBitmap();

    /**

    * @param bmp 获取的bitmap数据

    * @param picName 自定义的图片名

    */

    public void saveBmp2Gallery(Bitmap bmp, String picName) {

    String fileName =null;

    //系统相册目录

        String galleryPath= Environment.getExternalStorageDirectory()

    + File.separator + Environment.DIRECTORY_DCIM

                +File.separator+"Camera"+File.separator;

    // 声明文件对象

        File file =null;

    // 声明输出流

        FileOutputStream outStream =null;

    try {

    // 如果有目标文件,直接获得文件对象,否则创建一个以filename为名称的文件

            file =new File(galleryPath, picName+".jpg");

    // 获得文件相对路径

            fileName = file.toString();

    // 获得输出流,如果文件中有内容,追加内容

            outStream =new FileOutputStream(fileName);

    if (null != outStream) {

    bmp.compress(Bitmap.CompressFormat.JPEG,90, outStream);

    }

    }catch (Exception e) {

    e.getStackTrace();

    }finally {

    try {

    if (outStream !=null) {

    outStream.close();

    }

    }catch (IOException e) {

    e.printStackTrace();

    }

    }

    MediaStore.Images.Media.insertImage(this.getContentResolver(),

    bmp, fileName,null);

    // 插入系统图库,并生成一张缩略图

        Intent intent =new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);

    Uri uri = Uri.fromFile(file);

    intent.setData(uri);

    this.sendBroadcast(intent);

    Q_ToastUtil.showToast("图片保存成功");

    }

    转自https://www.jianshu.com/p/b598e18ad63c

    相关文章

      网友评论

          本文标题:2019-06-29

          本文链接:https://www.haomeiwen.com/subject/tuzycctx.html