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. }

更多相关文章

  1. android gallery相关操作
  2. Android常用屏幕适配方式
  3. View动画
  4. Android——修改开机画面
  5. [android]控件ImageView的常用属性
  6. Android(安卓)圆角图片,基于Glide4.9 的 BitmapTransformation,可
  7. View动画
  8. Android(安卓)动态logo bootanimation.zip 制作
  9. Android公共库——图片缓存 网络缓存 下拉及底部更多ListView 公

随机推荐

  1. Android6的Logger日志系统
  2. Android 中的拿来主义(编译,反编译,AXMLPri
  3. Android:使用pagerslidingtabstrip做Tab
  4. android bionic移植需要注意的事项
  5. android XMPP
  6. android sqlite相关
  7. android获取当前正在运行的栈顶的应用包
  8. Android开源项目分类汇总(六)工具库
  9. Android(安卓)-- Activity值传递
  10. 【Android测试工具】03. ApkTool在Mac上