android系统如何调用自带的相机相册
来源:互联网 发布:红鸟棋牌源码 编辑:程序博客网 时间:2024/04/28 00:26
android系统如何调用系统自带的相册和相机简单的代码:
1)第一种调用的方法相关代码:
<strong><span style="font-size:18px;">import java.io.IOException;import com.example.photo.R;import com.example.photo.ui.utils.PhotoUtils;import android.net.Uri;import android.os.Bundle;import android.provider.MediaStore;import android.text.TextUtils;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ImageView;import android.app.Activity;import android.app.AlertDialog;import android.content.DialogInterface;import android.content.Intent;import android.graphics.Bitmap;public class MainActivity extends Activity implements OnClickListener{private Button bt_select;private ImageView iv_select_images;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);iv_select_images = (ImageView) findViewById(R.id.iv_select_images);bt_select = (Button) findViewById(R.id.bt_select);bt_select.setOnClickListener(this);}@Overridepublic void onClick(View v) {//选择的itemfinal CharSequence[] items = {"相册","拍照"};AlertDialog dlg = new AlertDialog.Builder(MainActivity.this).setTitle("选择照片").setItems(items, new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {//这里item是根据选择的方式,在item数组里边定义了两种方式,拍照的下标为1所以就调用拍照的方法if(which==1){//调用android自带的照相机Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//Uri photoUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;startActivityForResult(intent, 1);}else{//调用android的图库 两种方法都可以 //第一种//Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);//startActivityForResult(i, 2);//第二种Intent i = new Intent(Intent.ACTION_GET_CONTENT);i.addCategory(Intent.CATEGORY_OPENABLE);i.setType("image/*");startActivityForResult(i, 2);}}}).create();dlg.show();}/** * 图片选取的相关代码 */@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {super.onActivityResult(requestCode, resultCode, data);//完成照相后回调用此方法//判断是否成功if(resultCode==MainActivity.RESULT_OK){//调用读取手机内存图片if(requestCode==2){Uri address = data.getData();if(!TextUtils.isEmpty(address.toString())){iv_select_images.setImageURI(address);}else{//用BitmapFactory去加载拍照前指定的路径位置的图片Bitmap bitmap;try {bitmap = PhotoUtils.revitionImageSize(address.toString());iv_select_images.setImageBitmap(bitmap);} catch (IOException e) {e.printStackTrace();}}}else if(requestCode==1){//调用本地相机拍照Bundle bundle = data.getExtras();//获取相机返回的数据,并转换成bitmap的图片格式Bitmap photo = bundle.getParcelable("data");iv_select_images.setImageBitmap(photo);}}}}</span></strong>
布局文件:
<RelativeLayout 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"
tools:context=".MainActivity" >
<Button
android:id="@+id/bt_select"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="#7Ab900"
android:text="打开" />
<ImageView
android:id="@+id/iv_select_images"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
2)第二种方法
package com.example.photo.ui;
import java.io.IOException;
import com.example.photo.R;
import com.example.photo.ui.utils.PhotoUtils;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class SecondActivity extends Activity{
private Button bt_select1,bt_select2;
private ImageView iv_select_images1;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
//初始化布局
setupView();
//设置点击事件
setupClick();
}
private void setupView() {
bt_select1 = (Button) findViewById(R.id.bt_select1);
bt_select2 = (Button) findViewById(R.id.bt_select2);
iv_select_images1 = (ImageView) findViewById(R.id.iv_select_images1);
}
private void setupClick() {
bt_select1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//调用android自带的相机
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.addCategory(Intent.CATEGORY_DEFAULT);
startActivityForResult(intent, 1);
}
});
bt_select2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
startActivityForResult(i, 2);
}
});
}
/**
* 图片选取的相关代码
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//完成照相后回调用此方法
//判断是否成功
if(resultCode==SecondActivity.RESULT_OK){
//调用读取手机内存图片
if(requestCode==2){
Uri address = data.getData();
if(!TextUtils.isEmpty(address.toString())){
iv_select_images1.setImageURI(address);
}else{
//用BitmapFactory去加载拍照前指定的路径位置的图片
Bitmap bitmap;
try {
bitmap = PhotoUtils.revitionImageSize(address.toString());
iv_select_images1.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}else if(requestCode==1){
//调用本地相机拍照
Bundle bundle = data.getExtras();
//获取相机返回的数据,并转换成bitmap的图片格式
Bitmap photo = bundle.getParcelable("data");
iv_select_images1.setImageBitmap(photo);
}
}
}
}
布局文件:
<RelativeLayout 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"
tools:context=".MainActivity" >
<Button
android:id="@+id/bt_select1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="#7Ab900"
android:text="打开" />
<ImageView
android:id="@+id/iv_select_images1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@drawable/ic_launcher" />
<Button
android:id="@+id/bt_select2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toLeftOf="@+id/iv_select_images1"
android:background="#7Ab900"
android:text="相册" />
</RelativeLayout>
- android系统如何调用自带的相机相册
- Android 调用系统自带的相机/拍照
- iOS开发:调用系统自带相机以及获取相册照片的功能实现
- Android 调用系统相机进行拍照 使用自带的图片选择器和裁剪工具
- 如何调用android系统自带的一些方法
- Android 调用系统相机以及相册源码
- Android 调用系统相机以及相册源码
- android 调用系统相机和相册
- Android调用系统相机和相册
- Android调用系统相机和相册-填坑篇
- Android调用系统相机和相册
- Android调用系统相机和相册
- android调用系统相机和相册
- Android调用系统相机和相册-坑篇
- Android调用系统相机和相册
- android调用系统相机、相册、裁剪
- android 调用系统相机、相册剪切功能
- Android 调用系统相机和相册
- 微信分身,微信多开,微信双开
- 人脸检测研究2015最新进展
- Java String 源码解析
- Java 入门 之 控制台输出乱码
- C# 异步非阻塞的接口实现
- android系统如何调用自带的相机相册
- 使用栈的二叉树前序与中序遍历
- lightoj 1234 - Harmonic Number 【分块打表】
- Cisco 3800 路由器配置实例
- Java源码阅读之Executors
- Java中HashTable和HashMap的区别
- Java基础
- Android volley 解析(四)之缓存篇
- sqlite存储图片