android相机调用及存储详解

  1 package com.firefly.six;
  2 
  3 import java.io.File;
  4 import java.io.FileInputStream;
  5 import java.io.FileNotFoundException;
  6 import java.text.SimpleDateFormat;
  7 import java.util.ArrayList;
  8 import java.util.Date;
  9 import java.util.List;
 10 
 11 import android.app.Activity;
 12 import android.content.Intent;
 13 import android.graphics.Bitmap;
 14 import android.graphics.BitmapFactory;
 15 import android.graphics.drawable.Drawable;
 16 import android.net.Uri;
 17 import android.os.Bundle;
 18 import android.os.Environment;
 19 import android.provider.MediaStore;
 20 import android.util.Log;
 21 import android.view.View;
 22 import android.view.ViewGroup;
 23 import android.view.Window;
 24 import android.widget.BaseAdapter;
 25 import android.widget.GridView;
 26 import android.widget.ImageButton;
 27 import android.widget.ImageView;
 28 import android.widget.TextView;
 29 import android.widget.Toast;
 30 
 31 public class ImageActivity extends Activity {
 32     File outputImage;
 33     private ImageView iv;
 34     public static final int TAKE_PHOTO = 1;
 35     public static final int CROP_PHOTO = 2;
 36     private Uri imageUri;
 37     private String path = Environment.getExternalStorageDirectory()
 38             .getAbsolutePath();
 39     private String imagePath;
 40     private GridView gv_photos;
 41     String photoPath = path + "/Six/image/";
 42     File[] images;
 43     ImageButton image;
 44     List<Drawable> lists;
 45 
 46     @Override
 47     protected void onCreate(Bundle savedInstanceState) {
 48         super.onCreate(savedInstanceState);
 49         requestWindowFeature(Window.FEATURE_NO_TITLE);
 50         setContentView(R.layout.activity_image);
 51         init();
 52     }
 53 
 54     private void init() {
 55         iv = (ImageView) findViewById(R.id.iv_picture);
 56         File f = new File(photoPath);
 57         images = f.listFiles();
 58         try {
 59             lists = new ArrayList<Drawable>();
 60             for (int i = 0; i < images.length; i++) {
 61                 FileInputStream fil = new FileInputStream(photoPath
 62                         + images[i].getName());
 63                 Drawable d = Drawable.createFromStream(fil, null);
 64                 lists.add(d);
 65             }
 66 
 67         } catch (FileNotFoundException e) {
 68             // TODO Auto-generated catch block
 69             e.printStackTrace();
 70         }
 71 
 72         gv_photos = (GridView) findViewById(R.id.gv_photos);
 73         gv_photos.setAdapter(new BaseAdapter() {
 74 
 75             @Override
 76             public View getView(int position, View convertView, ViewGroup parent) {
 77                 // TODO Auto-generated method stub
 78                 convertView = View.inflate(ImageActivity.this,
 79                         R.layout.item_photo, null);
 80                 ImageView iv = (ImageView) convertView
 81                         .findViewById(R.id.iv_photo_item);
 82                 TextView tv = (TextView) convertView
 83                         .findViewById(R.id.tv_photo);
 84                 tv.setText(images[position].getName());
 85                 iv.setImageDrawable(lists.get(position));
 86                 return convertView;
 87             }
 88 
 89             @Override
 90             public long getItemId(int position) {
 91                 // TODO Auto-generated method stub
 92                 return 0;
 93             }
 94 
 95             @Override
 96             public Object getItem(int position) {
 97                 // TODO Auto-generated method stub
 98                 return null;
 99             }
100 
101             @Override
102             public int getCount() {
103                 // TODO Auto-generated method stub
104                 return lists.size();
105             }
106         });
107     }
108 
109     public void SelectImage(View view) {
110         // 创建File路径,用于存储拍照后的图片
111 
112         // File filePath = Environment.getExternalStorageDirectory();
113 
114         // 新建此文件夹
115         File fileFolder = new File(photoPath);
116         if (!fileFolder.exists() && !fileFolder.isDirectory()) {
117             fileFolder.mkdirs();
118         }
119 
120         // 文件名不能总是同一个名字,所以在这里,可以选择用时间来做图片名称
121 
122         SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
123         Date curDate = new Date(System.currentTimeMillis());// 获取当前时间
124         String name = "six" + formatter.format(curDate);
125         // 新建此文件
126         outputImage = new File(fileFolder, name + ".jpg");
127         // 得到它的名字,在显示中用
128         imagePath = photoPath + name + ".jpg";
129         // 找到地址及调用
130         imageUri = Uri.fromFile(outputImage);
131         Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
132         intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
133         // 启动相机程序
134         startActivityForResult(intent, TAKE_PHOTO);
135 
136     }
137 
138     // 注意,这里是要重写这个方法,必须要重写!!
139     @Override
140     public void onActivityResult(int requestCode, int resultCode, Intent data) {
141         switch (requestCode) {
142         case TAKE_PHOTO:
143             if (resultCode == RESULT_OK) {
144                 Intent intent = new Intent("com.android.camera.action.CROP");
145                 intent.setDataAndType(imageUri, "image/*");
146                 intent.putExtra("scale", true);
147                 intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
148                 // 启动裁剪程序
149                 startActivityForResult(intent, CROP_PHOTO);
150             }
151             break;
152         case CROP_PHOTO:
153             if (resultCode == RESULT_OK) {
154                 try {
155                     Bitmap bitmap = BitmapFactory
156                             .decodeStream(getContentResolver().openInputStream(
157                                     imageUri));
158                     // 将裁剪后的照片显示出来
159                     iv.setImageBitmap(bitmap);
160                 } catch (FileNotFoundException e) {
161                     e.printStackTrace();
162                 }
163             }
164             break;
165         default:
166             break;
167         }
168     }
169 
170     public void PhotoToImage(View view) {
171         Toast.makeText(ImageActivity.this, "未做待续哈~", 0).show();
172     }
173 
174 }

 

posted @ 2016-05-08 19:09  春风自是人间客  阅读(721)  评论(0编辑  收藏  举报