图片压缩保存读取操作
16lz
2021-12-04
- //对图片进行压缩
- 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 gallery相关操作
- Android常用屏幕适配方式
- View动画
- Android——修改开机画面
- [android]控件ImageView的常用属性
- Android(安卓)圆角图片,基于Glide4.9 的 BitmapTransformation,可
- View动画
- Android(安卓)动态logo bootanimation.zip 制作
- Android公共库——图片缓存 网络缓存 下拉及底部更多ListView 公