图片压缩保存读取操作
16lz
2021-01-23
- //对图片进行压缩
- BitmapFactory.Optionsoptions=newBitmapFactory.Options();
- options.inJustDecodeBounds=true;
- //获取这个图片的宽和高
- Bitmapbitmap=BitmapFactory.decodeFile("/sdcard/dcim/Camera/hello.jpg",options);//此时返回bm为空
- options.inJustDecodeBounds=false;
- //计算缩放比
- intbe=(int)(options.outHeight/(float)200);
- if(be<=0)
- be=1;
- options.inSampleSize=be;
- //重新读入图片,注意这次要把options.inJustDecodeBounds设为false哦
- bitmap=BitmapFactory.decodeFile("/sdcard/dcim/Camera/hello.jpg",options);
- intw=bitmap.getWidth();
- inth=bitmap.getHeight();
- System.out.println(w+""+h);
- myImageView.setImageBitmap(bitmap);
- //保存入sdCard
- Filefile2=newFile("/sdcard/dcim/Camera/test.jpg");
- try{
- FileOutputStreamout=newFileOutputStream(file2);
- if(bitmap.compress(Bitmap.CompressFormat.JPEG,100,out)){
- out.flush();
- out.close();
- }
- }catch(Exceptione){
- //TODO:handleexception
- }
- //读取sd卡
- Filefile=newFile("/sdcard/dcim/Camera/test.jpg");
- intmaxBufferSize=16*1024;
- intlen=0;
- ByteArrayOutputStreamoutStream=newByteArrayOutputStream();
- BufferedInputStreambufferedInputStream;
- try{
- bufferedInputStream=newBufferedInputStream(newFileInputStream(file));
- intbytesAvailable=bufferedInputStream.available();
- intbufferSize=Math.min(bytesAvailable,maxBufferSize);
- byte[]buffer=newbyte[bufferSize];
- while((len=bufferedInputStream.read(buffer))!=-1)
- {
- outStream.write(buffer,0,bufferSize);
- }
- data=outStream.toByteArray();
- outStream.close();
- bufferedInputStream.close();
- }catch(FileNotFoundExceptione){
- e.printStackTrace();
- }catch(IOExceptione){
- e.printStackTrace();
- }
更多相关文章
- Android 圆角图片,基于Glide4.9 的 BitmapTransformation,可任意设
- 【原创】Android 4.4前后版本读取图库图片方式的变化
- Android公共库——图片缓存 网络缓存 下拉及底部更多ListView 公
- Android RadioButton背景图片和文字的相对位置
- Android中高效的显示图片之一 ——加载大图
- 华为android 10 手机恢复删除的图片,目前只能恢复小图。
- 设置提示信息,输入值类型,输入框引入图片,设置输入框的形状
- 从Android读取Unity assets下的图片并保存为Bitmap格式
- 开机动画(闪动的ANDROID字样的动画图片)