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;
@Override
protected 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);
}
@Override
public void onClick(View v) {
//選擇的item
final CharSequence[] items = {"相冊","拍照"};
AlertDialog dlg = new AlertDialog.Builder(MainActivity.this).setTitle("選擇照片").setItems(items,
new DialogInterface.OnClickListener() {
@Override
public 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();
}
/**
* 圖片選取的相關代碼
*/
@Override
protected 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>