图片压缩保存读取操作

  1. //对图片进行压缩
  2. BitmapFactory.Optionsoptions=newBitmapFactory.Options();
  3. options.inJustDecodeBounds=true;
  4. //获取这个图片的宽和高
  5. Bitmapbitmap=BitmapFactory.decodeFile("/sdcard/dcim/Camera/hello.jpg",options);//此时返回bm为空
  6. options.inJustDecodeBounds=false;
  7. //计算缩放比
  8. intbe=(int)(options.outHeight/(float)200);
  9. if(be<=0)
  10. be=1;
  11. options.inSampleSize=be;
  12. //重新读入图片,注意这次要把options.inJustDecodeBounds设为false哦
  13. bitmap=BitmapFactory.decodeFile("/sdcard/dcim/Camera/hello.jpg",options);
  14. intw=bitmap.getWidth();
  15. inth=bitmap.getHeight();
  16. System.out.println(w+""+h);
  17. myImageView.setImageBitmap(bitmap);
  18. //保存入sdCard
  19. Filefile2=newFile("/sdcard/dcim/Camera/test.jpg");
  20. try{
  21. FileOutputStreamout=newFileOutputStream(file2);
  22. if(bitmap.compress(Bitmap.CompressFormat.JPEG,100,out)){
  23. out.flush();
  24. out.close();
  25. }
  26. }catch(Exceptione){
  27. //TODO:handleexception
  28. }
  1. //读取sd卡
  2. Filefile=newFile("/sdcard/dcim/Camera/test.jpg");
  3. intmaxBufferSize=16*1024;
  4. intlen=0;
  5. ByteArrayOutputStreamoutStream=newByteArrayOutputStream();
  6. BufferedInputStreambufferedInputStream;
  7. try{
  8. bufferedInputStream=newBufferedInputStream(newFileInputStream(file));
  9. intbytesAvailable=bufferedInputStream.available();
  10. intbufferSize=Math.min(bytesAvailable,maxBufferSize);
  11. byte[]buffer=newbyte[bufferSize];
  12. while((len=bufferedInputStream.read(buffer))!=-1)
  13. {
  14. outStream.write(buffer,0,bufferSize);
  15. }
  16. data=outStream.toByteArray();
  17. outStream.close();
  18. bufferedInputStream.close();
  19. }catch(FileNotFoundExceptione){
  20. e.printStackTrace();
  21. }catch(IOExceptione){
  22. e.printStackTrace();
  23. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值