BitmapFactory.Options

加载和显示图片是很消耗内存的一件事,BitmapFactory.Options 类,  允许我们定义图片以何种方式如何读到内存,

  1. BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();  
  2. bmpFactoryOptions.inSampleSize = 8;  
  3. Bitmap bmp = BitmapFactory.decodeFile(imageFilePath, bmpFactoryOptions);  
  4. imv.setImageBitmap(bmp);  

上面的代码使图片变成原来的1/8.


  1. //imv = (ImageView) findViewById(R.id.ReturnedImageView);  
  2.         Display currentDisplay = getWindowManager().getDefaultDisplay();  
  3.         int dw = currentDisplay.getWidth();  
  4.         int dh = currentDisplay.getHeight();  
  5.           try  
  6.        {  
  7.         BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();  
  8.         bmpFactoryOptions.inJustDecodeBounds = true;  
  9.         Bitmap bmp = BitmapFactory.decodeStream(getContentResolver().  
  10.                 openInputStream(imageFileUri), null,  bmpFactoryOptions);  
  11.   
  12.         int heightRatio = (int)Math.ceil(bmpFactoryOptions.outHeight/(float)dh);  
  13.         int widthRatio = (int)Math.ceil(bmpFactoryOptions.outWidth/(float)dw);  
  14.   
  15.         Log.v("HEIGHRATIO"""+heightRatio);  
  16.         Log.v("WIDTHRATIO"""+widthRatio);  
  17.   
  18.         if (heightRatio > 1 && widthRatio > 1)  
  19.         {  
  20.             bmpFactoryOptions.inSampleSize =  heightRatio > widthRatio ? heightRatio:widthRatio;  
  21.         }  
  22.         bmpFactoryOptions.inJustDecodeBounds = false;  
  23.         bmp = BitmapFactory.decodeStream(getContentResolver().  
  24.                 openInputStream(imageFileUri), null,  bmpFactoryOptions);  
  25.            returnedImageView.setImageBitmap(bmp);  
  26.        }  
  27.        catch (FileNotFoundException e)  
  28.        {  
  29.            Log.v("ERROR", e.toString());  
  30.   
  31.        }  


上面的代码让图片根据窗口大小改变
  1. bmpFactoryOptions.inJustDecodeBounds = true;  
这一行让代码只解码图片的Bounds
对于拍摄照片我们可以直接调用系统自带的相机拍照,一般情况下无需我们自己开发相机拍照。 1、当点击按钮后我们可以通过Intent意图启动系统相机 @Override public void onClick(View v) { Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE); String temName=new DateFormat().format("yyMMdd_hhmmss",System.currentTimeMillis())+"_"+(Math.random()*100)+".jpg"; //文件名 image_path=path+File.separator+temName; File file=new File(image_path); if(file.exists()){ file.delete(); } Uri u=Uri.fromFile(file); intent.putExtra(MediaStore.EXTRA_OUTPUT, u); startActivityForResult(intent, 0); } 在这里设置 intent.putExtra(MediaStore.EXTRA_OUTPUT, u);非常重要,如果不设置这个参数那么我们获取到的图片只是一个经过压缩后的缩略图,只有设置这个才能得到拍摄后的原图。 2、在startActivityForResult(intent, 0);后我们需要重写onActivityResult(int requestCode, int resultCode, Intent data)方法,如果设置了MediaStore.EXTRA_OUTPUT那么我们在这里data返回的是null。 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if(resultCode==RESULT_OK){ File file=new File(image_path); try { Uri uri = Uri.fromFile(file); BitmapFactory.Options options=new BitmapFactory.Options(); options.inJustDecodeBounds=true; BitmapFactory.decodeStream(getContentResolver().openInputStream(uri), null, options); options.inSampleSize=4; options.inJustDecodeBounds=false; Bitmap map=BitmapFactory.decodeStream(getContentResolver().openInputStream(uri), null, options); android.provider.MediaStore.Images.Media.insertImage(getContentResolver(), map, null, null); sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,uri)); image.setImageBitmap(map); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } 蓝色部分是对图片进行简单的压缩处理,如果不进行处理会出现内存溢出。 红色部分是将图片保存在DCIM文件夹下。 绿色部分是发生一个广播通知系统重新扫描制定文件,这样我们就可以在图库本地图片中看到拍摄的图片。 3、最后记得在清单文件中加入调用系统相机和保存文件权限 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.CAMERA"/>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值