这里介绍在Android中实现相机调取、拍照片、获取照片、存储新路径等已经打开相册、选择照片等功能
首先看一下界面,很简单
配置读取内存卡和调用照相头的功能
<!-- 使用网络权限 --><uses-permission android:name="android.permission.INTERNET"/><!-- 写sd卡的权限 --><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/><!-- 读sd卡权限 --><uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/><uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
下面是代码的主题
public class TakePhotos extends Activity implementsandroid.view.View.OnClickListener {Button takePhoto;Bitmap photo;String picPath;Button capture;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.activity_photo);takePhoto = (Button) findViewById(R.id.button1);capture = (Button) findViewById(R.id.capture);takePhoto.setOnClickListener(this);capture.setOnClickListener(this);}@Overridepublic void onClick(View viewid) {switch (viewid.getId()) {case R.id.button1: {// 打开相机String state = Environment.getExternalStorageState();// 获取内存卡可用状态if (state.equals(Environment.MEDIA_MOUNTED)) {// 内存卡状态可用Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");startActivityForResult(intent, 1);} else {// 不可用Toast.makeText(TakePhotos.this, "内存不可用", Toast.LENGTH_LONG).show();}break;}case R.id.capture: {// 打开相册// 打开本地相册Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);// 设定结果返回startActivityForResult(i, 2);break;}default:break;}}@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {// TODO Auto-generated method stubsuper.onActivityResult(requestCode, resultCode, data);if (data != null) {switch (requestCode) {case 1:// 两种方式 获取拍好的图片if (data.getData() != null || data.getExtras() != null) { // 防止没有返回结果Uri uri = data.getData();if (uri != null) {this.photo = BitmapFactory.decodeFile(uri.getPath()); // 拿到图片}if (photo == null) {Bundle bundle = data.getExtras();if (bundle != null) {photo = (Bitmap) bundle.get("data");FileOutputStream fileOutputStream = null;try {// 获取 SD 卡根目录 生成图片并String saveDir = Environment.getExternalStorageDirectory()+ "/dhj_Photos";// 新建目录File dir = new File(saveDir);if (!dir.exists())dir.mkdir();// 生成文件名SimpleDateFormat t = new SimpleDateFormat("yyyyMMddssSSS");String filename = "MT" + (t.format(new Date()))+ ".jpg";// 新建文件File file = new File(saveDir, filename);// 打开文件输出流fileOutputStream = new FileOutputStream(file);// 生成图片文件this.photo.compress(Bitmap.CompressFormat.JPEG,100, fileOutputStream);// 相片的完整路径this.picPath = file.getPath();ImageView imageView = (ImageView) findViewById(R.id.imageView1);imageView.setImageBitmap(this.photo);} catch (Exception e) {e.printStackTrace();} finally {if (fileOutputStream != null) {try {fileOutputStream.close();} catch (Exception e) {e.printStackTrace();}}}Toast.makeText(getApplicationContext(), "获取到了",Toast.LENGTH_SHORT).show();} else {Toast.makeText(getApplicationContext(), "找不到图片",Toast.LENGTH_SHORT).show();}}}break;case 2: {//打开相册并选择照片,这个方式选择单张// 获取返回的数据,这里是android自定义的Uri地址Uri selectedImage = data.getData();String[] filePathColumn = { MediaStore.Images.Media.DATA };// 获取选择照片的数据视图Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);cursor.moveToFirst();// 从数据视图中获取已选择图片的路径int columnIndex = cursor.getColumnIndex(filePathColumn[0]);String picturePath = cursor.getString(columnIndex);cursor.close();// 将图片显示到界面上ImageView imageView = (ImageView) findViewById(R.id.imageView1);imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));break;}default:break;}}}}
注释的很详细,自己分析吧。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持武林网。
新闻热点
疑难解答