直播app开发搭建,Android studio 图片压缩

直播app开发搭建,Android studio 图片压缩

获取图片目录

 

1
File file=Environment.getExternalStorageDirectory();//获取根路径 storage/emulated/0<br>String path1=file.getPath()+"/Pictures/1655215651628.jpg";//Pictures文件夹下面的 1655215651628.jpg图片名<br> 

1
<br>     //path 压缩图片的路径<br>        String path="/storage/emulated/0/Pictures/1655215651628.jpg";<br>        Bitmap bitmap = obtainImageFromPath(path, 100, 160);<br>        saveBitmapFile(bitmap);//把压缩图片储存<br>        image.setImageBitmap(bitmap);<br> //传入路径和宽高<br>    public static Bitmap obtainImageFromPath(String path, int width, int height) {<br>        BitmapFactory.Options o = new BitmapFactory.Options();<br>        o.inJustDecodeBounds = true;<br>        BitmapFactory.decodeFile(path, o);<br>        o.inSampleSize = calculateSampleSize(o, width, height);<br>        o.inJustDecodeBounds = false;<br>        return BitmapFactory.decodeFile(path, o);<br>    }<br>    //计算样本量<br>    private static int calculateSampleSize(BitmapFactory.Options o, int reqWidth, int reqHeight) {<br>        int sampleSize = 1;<br>        if (o.outWidth > reqWidth || o.outHeight > reqHeight) {<br>            final int halfWidth = o.outWidth / 2;<br>            final int halfHeight = o.outHeight / 2;<br>            while ((halfHeight / sampleSize) >= reqHeight<br>                    && (halfWidth / sampleSize) >= reqWidth) {<br>                sampleSize *= 2;<br>            }<br>        }<br>        return sampleSize;<br>    }<br>//保存压缩的文件<br>    public void saveBitmapFile(Bitmap bitmap){<br>        File file=new File("/storage/emulated/0/Pictures/1655215651628.jpg");//将要保存图片的路径,图片的大小已经改变<br>        try {<br>            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));<br>            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);<br>            bos.flush();<br>            bos.close();<br>        } catch (IOException e) {<br>            e.printStackTrace();<br>        }<br>    }

 以上就是 直播app开发搭建,Android studio 图片压缩,更多内容欢迎关注之后的文章

 

posted @   云豹科技-苏凌霄  阅读(27)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
历史上的今天:
2022-09-27 直播平台怎么搭建,实现js开光灯效果
2022-09-27 直播电商平台开发,顺序循环图片切换
2022-09-27 直播网站源码,点击按钮Button,更换背景颜色
2021-09-27 直播平台开发,操作成功后自动返回首页
2021-09-27 短视频app开发,点击视频进行全屏播放
2021-09-27 短视频直播源码,Picker数字滑动选择器
点击右上角即可分享
微信分享提示