3.1 使用内置Gallery应用程序选择图像

   为了使用一个预装Android应用程序中存在的功能,利用意图通常是最快捷的方式。出于介绍本章中示例的目的,让我们看看如何利用内置的Gallery(图像库)应用程序选择希望使用的图像。

   我们将要使用的意图是一个通用的Intent.ACTION_PICK,它通知Android:我们想要选择一块数据。同时,我们还提供了一个将要从中获取数据的URI。在当前情况下,使用android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI,这意味着会选择通过使用MediaStore存储在SD卡上的图像。

1   Intent choosePictureIntent=new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

  当触发这个意图时,它会以用户能够选择一幅图像的模式启动Gallery应用程序。

   当通常从意图返回一样,在用户选中图像之后将触发onActivityResult方法。在返回的意图数据中,将返回所选择图像的URI。

1     @Override
2     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
3         super.onActivityResult(requestCode, resultCode, data);
4         if(resultCode==RESULT_OK){
5             Uri imageFileUri=data.getData();
6         }
7     }

    以下是完整的示例:

 1 package com.nthm.androidtest;
 2 
 3 import java.io.FileNotFoundException;
 4 import android.app.Activity;
 5 import android.content.Intent;
 6 import android.graphics.Bitmap;
 7 import android.graphics.BitmapFactory;
 8 import android.net.Uri;
 9 import android.os.Bundle;
10 import android.view.Display;
11 import android.view.View;
12 import android.view.View.OnClickListener;
13 import android.widget.Button;
14 import android.widget.ImageView;

    我们的活动会响应由按钮触发的单击事件,因此将实现OnClickListener。在onCreate方法中,使用通常的findViewById方法访问在布局XML中定义的必要的UI元素。

 1   public class ChoosePicture extends Activity implements OnClickListener {
 2     private ImageView chosenImageView;
 3     private Button choosePicture;
 4     
 5     @Override
 6     protected void onCreate(Bundle savedInstanceState) {
 7         super.onCreate(savedInstanceState);
 8         setContentView(R.layout.choosepicture);
 9         chosenImageView=(ImageView) findViewById(R.id.chosenImageView);
10         choosePicture=(Button) findViewById(R.id.ChoosePictureButton);
11         choosePicture.setOnClickListener(this);
12     }

    随后是onClick方法,它将响应choosePicture按钮的单击事件,该按钮在应用程序启动时显示。这个方法创建意图,其将触发Gallery应用程序,使其以允许用户选择一张图片的模式启动。

1     @Override
2     public void onClick(View v) {
3         Intent choosePictureIntent=new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
4         startActivityForResult(choosePictureIntent, 0);
5     }

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

1     @Override
2     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
3         super.onActivityResult(requestCode, resultCode, data);
4         if(resultCode==RESULT_OK){
5             Uri imageFileUri=data.getData();

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

 1             Display currentDisplay=getWindowManager().getDefaultDisplay();
 2             int dw=currentDisplay.getWidth();
 3             int dh=currentDisplay.getHeight();
 4             BitmapFactory.Options bmpBitmapFactoryOptions=new BitmapFactory.Options();
 5             bmpBitmapFactoryOptions.inJustDecodeBounds=true;
 6             try {
 7                 Bitmap bmp=BitmapFactory.decodeStream(getContentResolver().openInputStream(imageFileUri),null,bmpBitmapFactoryOptions);
 8                 int heightRatio=(int)Math.ceil(bmpBitmapFactoryOptions.outHeight/(float)dh);
 9                 int widthRatio=(int)Math.ceil(bmpBitmapFactoryOptions.outWidth/(float)dw);
10                 if(heightRatio>1&&widthRatio>1){
11                     if(heightRatio>widthRatio){
12                         bmpBitmapFactoryOptions.inSampleSize=heightRatio;
13                     }else{
14                         bmpBitmapFactoryOptions.inSampleSize=widthRatio;
15                     }
16                 }
17                 bmpBitmapFactoryOptions.inJustDecodeBounds=false;
18                 bmp=BitmapFactory.decodeStream(getContentResolver().openInputStream(imageFileUri),null,bmpBitmapFactoryOptions);
19                 chosenImageView.setImageBitmap(bmp);
20             } catch (FileNotFoundException e) {
21                 e.printStackTrace();
22             }
23         }
24     }
25 }

    需要在项目的layout/choosepicture.xml文件中包含如下的内容:

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     android:layout_width="match_parent"
 3     android:layout_height="match_parent"
 4     android:orientation="vertical"
 5     >
 6     <Button 
 7         android:layout_width="fill_parent"
 8         android:layout_height="wrap_content"
 9         android:text="Choose Picture"
10         android:id="@+id/ChoosePictureButton" />
11     <ImageView 
12           android:layout_width="wrap_content"
13           android:layout_height="wrap_content"
14           android:id="@+id/chosenImageView"
15         />
16 </LinearLayout>

     以上就是所需的全部的内容,现在又一幅用户选择的图像作为位图对象显示给用户。

posted on 2014-08-22 17:30  宁静致远,一览众山小  阅读(405)  评论(0编辑  收藏  举报

导航