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

转自:http://blog.csdn.net/dyllove98/article/details/8831978

 

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

例如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 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值