手机拍照功能
2015-11-24 14:33
169 查看
xml
MainActivity.java
效果
<LinearLayout xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="takePhoto" android:id="@+id/take_photo"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="choicePhoto" android:id="@+id/choice_photo"/> <ImageView android:layout_width="match_parent" android:layout_height="600dp" android:id="@+id/my_photo"/> </LinearLayout>
MainActivity.java
package com.example.testsometing; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import javax.xml.transform.OutputKeys; import junit.framework.Protectable; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; public class MainActivity extends BaseActivity { Button btn_takePhoto; Button btn_choicephone; ImageView img_photo; Uri imageUri; File outputFile; static final int TAKE_PHOTO = 1; static final int CROP_PHOTO = 2; static final int CHOICE_PHONE = 3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn_takePhoto = (Button) this.findViewById(R.id.take_photo); img_photo = (ImageView) this.findViewById(R.id.my_photo); btn_takePhoto.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { outputFile = new File("/storage/emulated/0/DCIM/Camera","imgxPhoto.jpg"); if(outputFile.exists()){ outputFile.delete(); }else{ //不做处理 } try { outputFile.createNewFile(); } catch (IOException e) { e.printStackTrace(); } imageUri = Uri.fromFile(outputFile); Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri); //将所拍图片的路径设置为imagerUri,如果不做设置则为相机拍照的默认路径 //设置路径后“照片”是看不到所拍的图片,要进入“文件”才能看到所拍的照片 startActivityForResult(intent, TAKE_PHOTO); } }); btn_choicephone = (Button) this.findViewById(R.id.choice_photo); btn_choicephone.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { File file = new File("/storage/emulated/0/DCIM/Camera","myphotoy.jpg"); if(file.exists()){ file.delete(); } else{ //不做处理 } try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); } imageUri = Uri.fromFile(file); Intent intent = new Intent("android.intent.action.GET_CONTENT"); intent.setType("image/*"); //不设置Type会报错 intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); /* intent.putExtra("scale", true);//规模,要不要都一样,暂时看不出区别 */ intent.putExtra("crop",true);//可以剪切 startActivityForResult(intent, CHOICE_PHONE); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { case TAKE_PHOTO: if(resultCode == RESULT_OK){ Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(imageUri,"image"); /*intent.putExtra("scale",true);*/ intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri); startActivityForResult(intent, CROP_PHOTO); } break; case CROP_PHOTO: if(resultCode == RESULT_OK){ try { Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri)); //如果对所拍的照片不做剪切处理则bitmap不能得到目标,因为所拍图片太大 img_photo.setImageBitmap(bitmap); } catch (FileNotFoundException e) { e.printStackTrace(); } } break; case CHOICE_PHONE: if(resultCode == RESULT_OK){ Bitmap bitmap; try { bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri)); //如果对所拍的照片不做剪切处理则bitmap不能得到目标,因为所拍图片太大 img_photo.setImageBitmap(bitmap); } catch (FileNotFoundException e) { e.printStackTrace(); } } break; default: break; } } }
效果
相关文章推荐
- Android开发 Inbox下拉刷新效果
- Linux中GCC源码编译安装
- 现在开始使用 ES6
- Android studio mac/windows 常用快捷键
- FTP服务学习笔记之FTP简介(1)
- equals和==
- 【数据库】机房收费系统数据库设计
- ubuntu命令之passwd
- eclipse 自动 注释
- Coderforce #332 C题
- Hdoj 1384 Intervals
- 利用keepalived 实现HA&LB
- HttpClient 发送网络请求步骤
- Incorrect string value:"\ \ \ \ \"for column'trurname' at row 1
- Oracle 百万行数据查询优化
- Android: Hide Navigation bar 隐藏导航条
- MyEclipse 2015 移动开发教程
- 对象引用与对象的区别
- SQL日期处理集锦
- selenium 怎么处理display:none