正文

使用内置Gallery应用程序选择图像(3)

Android多媒体开发高级编程 作者:(美)艾佛瑞


当Gallery应用程序在用户选择图像之后返回时,将调用onActivityResult方法。在传递到意图的数据中,可以得到所选择图像的URI。

     protected void onActivityResult(int requestCode, int resultCode,

Intent intent){

     super.onActivityResult(requestCode, resultCode, intent);

     if (resultCode == RESULT_OK) {

          Uri imageFileUri = intent.getData();

由于返回的图像可能太大而无法完全加载到内存中,因此在加载图像时将使用在第1章中介绍过的技术对其大小进行调整。整数dw和dh分别表示最大宽度和高度。最大高度小于屏幕高度的一半,因为最终将会以垂直对齐的方式显示两幅图像。

          Display currentDisplay = getWindowManager().getDefaultDisplay();

          int dw = currentDisplay.getWidth();

          int dh = currentDisplay.getHeight()/2 - 100;

          try {

               //加载图像的尺寸而非图像本身

               BitmapFactory.Options bmpFactoryOptions = 

new BitmapFactory.Options();

               bmpFactoryOptions.inJustDecodeBounds = true;

               Bitmap bmp = BitmapFactory.decodeStream(getContentResolver().

               openInputStream(imageFileUri), null, bmpFactoryOptions);

               int heightRatio = (int)Math.ceil(bmpFactoryOptions.

outHeight/(float)dh);

               int widthRatio = (int)Math.ceil(bmpFactoryOptions.

outWidth/(float)dw);


上一章目录下一章

Copyright © 读书网 www.dushu.com 2005-2020, All Rights Reserved.
鄂ICP备15019699号 鄂公网安备 42010302001612号