打开手机图库和打开照相机返回选中的图片简单小运用
2016-01-16 00:00
351 查看
打开手机图库和打开照相机返回选中的图片简单小运用
功能:界面有2个按钮 1、选择照片 2、拍摄照片 把选择的照片或者拍摄的照片显示在该页面
//1 -- 布局界面 activity_main.xml
代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="选择图片"
android:onClick="choose_image"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="拍摄照片"
android:onClick="camere"
/>
</LinearLayout>
-------------------------
//2 -- MainActivity 类
代码
public class MainActivity extends Activity {
private ImageView image;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image = (ImageView) this.findViewById(R.id.imageview);
}
//打开图库 选择 图片 按钮 事件 监听
public void choose_image(View view){
Intent intent = new Intent();
//打开图库
intent.setAction(intent.ACTION_PICK);
//设置图片的类型 -- 全部类型/*
intent.setType("image/*");
int requestCode = 1;
//用带返回结果的启动跳转方法
startActivityForResult(intent, requestCode );
}
//拍摄 照片 按钮 事件 监听
public void camere(View view){
Intent intent = new Intent();
//打开相机
intent.setAction("android.media.action.IMAGE_CAPTURE");
int requestCode = 2;
startActivityForResult(intent, requestCode );
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(data == null || resultCode !=Activity.RESULT_OK){
Toast.makeText(this, "没有选择图片!", Toast.LENGTH_SHORT).show();
return ;
}
if(requestCode == 1){
//用getData方法 获取 从图库 返回来的 数据
Uri uri = data.getData();
//用 ContentResolver 解析器 解析返回来的 数据
ContentResolver cr = getContentResolver();
try {
//把返回来的数据 给 openInputStream 进行 解析
InputStream is = cr.openInputStream(uri);
//解析工厂 对流 is 对象 进行 解析
Bitmap bitmap = BitmapFactory.decodeStream(is);
image.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(requestCode == 2){
//获取 拍摄的数据 -- 方法getParcelableExtra
Bitmap bitmap = data.getParcelableExtra("data");
image.setImageBitmap(bitmap);
}
}
}
功能:界面有2个按钮 1、选择照片 2、拍摄照片 把选择的照片或者拍摄的照片显示在该页面
//1 -- 布局界面 activity_main.xml
代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="选择图片"
android:onClick="choose_image"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="拍摄照片"
android:onClick="camere"
/>
</LinearLayout>
-------------------------
//2 -- MainActivity 类
代码
public class MainActivity extends Activity {
private ImageView image;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image = (ImageView) this.findViewById(R.id.imageview);
}
//打开图库 选择 图片 按钮 事件 监听
public void choose_image(View view){
Intent intent = new Intent();
//打开图库
intent.setAction(intent.ACTION_PICK);
//设置图片的类型 -- 全部类型/*
intent.setType("image/*");
int requestCode = 1;
//用带返回结果的启动跳转方法
startActivityForResult(intent, requestCode );
}
//拍摄 照片 按钮 事件 监听
public void camere(View view){
Intent intent = new Intent();
//打开相机
intent.setAction("android.media.action.IMAGE_CAPTURE");
int requestCode = 2;
startActivityForResult(intent, requestCode );
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(data == null || resultCode !=Activity.RESULT_OK){
Toast.makeText(this, "没有选择图片!", Toast.LENGTH_SHORT).show();
return ;
}
if(requestCode == 1){
//用getData方法 获取 从图库 返回来的 数据
Uri uri = data.getData();
//用 ContentResolver 解析器 解析返回来的 数据
ContentResolver cr = getContentResolver();
try {
//把返回来的数据 给 openInputStream 进行 解析
InputStream is = cr.openInputStream(uri);
//解析工厂 对流 is 对象 进行 解析
Bitmap bitmap = BitmapFactory.decodeStream(is);
image.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(requestCode == 2){
//获取 拍摄的数据 -- 方法getParcelableExtra
Bitmap bitmap = data.getParcelableExtra("data");
image.setImageBitmap(bitmap);
}
}
}
相关文章推荐
- AsyncTask 工具类的简单使用day8
- 使用ProgressBar和AsyncTask 下载图片
- 使用Spinner和AsyncTask 下载数据绑定到Spinner
- 使用AutoCompleteTextView和AsyncTask 检索城市
- ListView 的简单使用day9
- ListView和SimPleteAdapter 把新闻数据绑定到ListView
- BaseAdapter 简单使用day9.1
- ListView和BaseAdapter 把新闻数据添加到ListView
- ListView 加载下一页的简单功能
- 从网上绑定新闻数据到ListView
- ListView和CheckBox 的简单使用
- mysql排序
- 0001 -- 搭建bootstrap环境。
- 0002 -- bootstrap 图标的使用。
- 0003 -- bootstrap 按钮的使用。
- Linux学习笔记
- STL编程题1(C++程序设计第1周)
- 【HTML打卡】0115 margin重叠、内联元素、css控制段落
- [caffe]深度学习之图像分类模型VGG解读
- 构建灵活的界面