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 圆角图片,基于Glide4.9 的 BitmapTransformation,可任意设
  2. 【原创】Android 4.4前后版本读取图库图片方式的变化
  3. Android公共库——图片缓存 网络缓存 下拉及底部更多ListView 公
  4. Android RadioButton背景图片和文字的相对位置
  5. Android中高效的显示图片之一 ——加载大图
  6. 华为android 10 手机恢复删除的图片,目前只能恢复小图。
  7. 设置提示信息,输入值类型,输入框引入图片,设置输入框的形状
  8. 从Android读取Unity assets下的图片并保存为Bitmap格式
  9. 开机动画(闪动的ANDROID字样的动画图片)

随机推荐

  1. 《Android第一行代码》first reading 二
  2. Android Training - 创建一个Android工程
  3. Google手机操作系统Android将100%开源
  4. 第一行代码Android第一课
  5. Android root权限获取大揭秘
  6. Android 下载的三种实现方式(文件流的读
  7. 1.5.20 Android Adapter 用法总结
  8. Android(安卓)UI【android 自定义dialog
  9. Android蓝牙开发(一)
  10. 【Android读书笔记】Android如何知道Inte