现在的位置: 首页 > 综合 > 正文

Android获取图片资源之——拍照后在程序中显示照片

2013年05月17日 ⁄ 综合 ⁄ 共 2808字 ⁄ 字号 评论关闭

在手机应用程序中,用户选择图片有很多方式可以选择。

例如1、SD卡选择;2、通过拍照;3、通过网络搜索。

通过拍照来直接取得图片资源,实现原理很简单,这里简单说一下。

首先声明权限:

   <uses-permission android:name="android.permission.CAMERA"/>  
    <uses-permission android:name="android.permission.INTERNET" />  
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />  
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

然后布局文件:

    <Button  
        android:id="@+id/button"  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:text="点击按钮拍照"  
     />  
    <ImageView   
        android:id="@+id/imageView"  
        android:layout_below="@id/button"  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
     /> 

最后实现:

代码流程:用户触发拍照事件—>在SD卡中创建图片文件(一般是在相册中创建,当然也可以在自己应用程序的资源目录下创建)—>通过Intent调用手机照相功能,并且将刚创建的照片文件处理后放到intent参数中 即:

intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mPhotoFile));

。最后在调用activity返回事件(onActivityResult)时,根据照片路径(之前创建图片时用全局变量记住),通过BitmapFactory.decodeFile()读取照片,在程序中显示。

 

 1 package com.example.paizhao;
 2 
 3 import java.io.File;
 4 import java.text.SimpleDateFormat;
 5 import java.util.Date;
 6 import android.app.Activity;
 7 import android.content.Intent;
 8 import android.graphics.Bitmap;
 9 import android.graphics.BitmapFactory;
10 import android.net.Uri;
11 import android.os.Bundle;
12 import android.provider.MediaStore;
13 import android.view.View;
14 import android.widget.Button;
15 import android.widget.ImageView;
16 
17 public class MainActivity extends Activity {
18     private Button mButton;
19     private ImageView mImageView;//用于显示照片
20     private File mPhotoFile;
21     private String mPhotoPath;
22     public final static int CAMERA_RESULT = 8888;
23     public final static String TAG = "xx";
24 
25     @Override
26     public void onCreate(Bundle savedInstanceState) {
27         super.onCreate(savedInstanceState);
28         setContentView(R.layout.main);
29         mButton = (Button) findViewById(R.id.button);
30         mButton.setOnClickListener(new ButtonOnClickListener());
31         mImageView = (ImageView) findViewById(R.id.imageView);
32     }
33 
34     private class ButtonOnClickListener implements View.OnClickListener {
35         public void onClick(View v) {
36             try {
37                 Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
38                 mPhotoPath = "mnt/sdcard/DCIM/Camera/" + getPhotoFileName();
39                 mPhotoFile = new File(mPhotoPath);
40                 if (!mPhotoFile.exists()) {
41                     mPhotoFile.createNewFile();
42                 }
43                 intent.putExtra(MediaStore.EXTRA_OUTPUT,
44                         Uri.fromFile(mPhotoFile));
45                 startActivityForResult(intent, CAMERA_RESULT);
46             } catch (Exception e) {
47             }
48         }
49     }
50 
51     /**
52      * 用时间戳生成照片名称
53      * @return
54      */
55     private String getPhotoFileName() {
56         Date date = new Date(System.currentTimeMillis());
57         SimpleDateFormat dateFormat = new SimpleDateFormat(
58                 "'IMG'_yyyyMMdd_HHmmss");
59         return dateFormat.format(date) + ".jpg";
60     }
61 
62     @Override
63     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
64         super.onActivityResult(requestCode, resultCode, data);
65         if (requestCode == CAMERA_RESULT) {
66             Bitmap bitmap = BitmapFactory.decodeFile(mPhotoPath, null);
67             mImageView.setImageBitmap(bitmap);
68         }
69     }
70 }

代码不多,很简单吧。。。。

 

抱歉!评论已关闭.