//对图片进行压缩   BitmapFactory.Options options = new BitmapFactory.Options();   options.inJustDecodeBounds = true;      //获取这个图片的宽和高   Bitmap bitmap = BitmapFactory.decodeFile("/sdcard/dcim/Camera/hello.jpg",options);//此时返回bm为空   options.inJustDecodeBounds =false;   //计算缩放比   int be = (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);   int w = bitmap.getWidth();   int h=bitmap.getHeight();   System.out.println(w+" "+h);   myImageView.setImageBitmap(bitmap);         //保存入sdCard   File file2= new File("/sdcard/dcim/Camera/test.jpg");   try {FileOutputStream out = new FileOutputStream(file2);if(bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out)){out.flush();out.close();}} catch (Exception e) {// TODO: handle exception}      //读取sd卡   File file =new File("/sdcard/dcim/Camera/test.jpg");   int maxBufferSize = 16 * 1024;int len = 0;ByteArrayOutputStream outStream = new ByteArrayOutputStream(); BufferedInputStream bufferedInputStream;try {bufferedInputStream = new BufferedInputStream(new FileInputStream(file));int bytesAvailable = bufferedInputStream.available();int bufferSize = Math.min(bytesAvailable, maxBufferSize);byte[] buffer = new byte[bufferSize];while ((len = bufferedInputStream.read(buffer)) != -1){outStream.write(buffer, 0, bufferSize);} data = outStream.toByteArray();outStream.close();bufferedInputStream.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}
最近做项目,碰到一个问题,就是上传大文件,会内存溢出,我只能压缩图片进行上传啦。 这样我们就可以读取较大的图片而不会内存溢出了。

更多相关文章

  1. Android(安卓)利用TransitionDrawable 实现两张图片渐变切换
  2. android 网络图片与网页读取
  3. Android(安卓)File 数据存储
  4. Android(安卓)获取SDCard上图片和视频的缩略图
  5. Android(安卓)Log Analysis
  6. 如何为Android(安卓)Activity设置背景图片?
  7. Android读取联系人的姓名及号码
  8. android创建数据库(SQLite)保存图片示例
  9. Android(安卓)APP 设置启动的第一张图片

随机推荐

  1. 在一个activity中添加多个listview
  2. GitHub 标星 8K+!一款开源替代 ls 的工具
  3. Android Camera系列开发 (二)通过Intent
  4. android layout for gravity 详解
  5. Android各个版本之间的区别(二)
  6. Nginx系列教程(六)| 手把手教你搭建 LNMP
  7. Android 4.4KitKat AudioTrack 流程分析
  8. Android无线调试:tcpip无线连接 | WiFi ap
  9. Nginx系列教程(二)| 一文带你读懂Nginx的正
  10. Android之SQlite存储