本文为大家分享了Android相机、图册基本demo,供大家参考,具体内容如下
package com.example.democamera;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.widget.ImageView;
public class MainActivity extends Activity {
private ImageView iv;
static final int gallery = 1, camera = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (ImageView) findViewById(R.id.iv_picture);
}
public void startGallery(View view) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("image
public void startCamera(View view) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (isSDExsit()) {
File dir = new File(Environment.getExternalStorageDirectory()
+ "/my");
if (!dir.exists())
dir.mkdirs();
File file = new File(dir, "aaa.jpg");
intent.putExtra(MediaStore.EXTRA_SCREEN_ORIENTATION,
Configuration.ORIENTATION_UNDEFINED);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
}
startActivityForResult(intent, camera);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case gallery:
if (data != null) {
Uri uri = data.getData();
iv.setImageURI(uri);
Bitmap bitmap = BitmapFactory
.decodeFile(getPathByUri(uri, this));
iv.setImageBitmap(bitmap);
Bitmap bitmap2 = getBitmapByUri(uri, this);
iv.setImageBitmap(bitmap2);
}
break;
case camera:
if (data != null) {// 这是获得缩略图的方法
Bitmap b = (Bitmap) data.getExtras().get("data");
iv.setImageBitmap(b);
} else {// 有sd卡得到图片原图
Bitmap bitmap = BitmapFactory.decodeFile("sdcard/my/aaa.jpg");//直接这样做会有发生OOM的风险,Demo简单这么处理
iv.setImageBitmap(bitmap);
}
break;
default:
break;
}
}
public static Bitmap getBitmapByUri(Uri uri, Context context) {
Bitmap bitmap = null;
try {
bitmap = MediaStore.Images.Media.getBitmap(
context.getContentResolver(), uri);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return bitmap;
}
public static String getPathByUri(Uri uri, Context context) {
String path = null;
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = context.getContentResolver().query(uri, projection,
null, null, null);
if (cursor.moveToFirst()) {
int index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
path = cursor.getString(index);
}
return path;
}
public static boolean isSDExsit() {
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
return true;
} else {
return false;
}
}
}
下面分享具体Android 调用相机、打开相册、裁剪图片的实现代码,内容如下
private ImageView iv_user_photo;
private String fileName = "";
private File tempFile;
private int crop = 300;// 裁剪大小
private static final int OPEN_CAMERA_CODE = 10;
private static final int OPEN_GALLERY_CODE = 11;
private static final int CROP_PHOTO_CODE = 12;
private OnClickListener PopupWindowItemOnClick = new OnClickListener() {
@Override
public void onClick(View v) {
menuWindow.dismiss();
switch (v.getId()) {
// 拍照
case R.id.btn_camera:
initFile();
openCamera();
break;
// 相册
case R.id.btn_gallery:
initFile();
openGallery();
break;
default:
break;
}
}
};
public void initFile() {
if(fileName.equals("")) {
if(FileUtil.existSDCard()) {
String path = Environment.getExternalStorageDirectory() + File.separator + "JanuBookingOnline" + File.separator;
FileUtil.mkdir(path);
Logger.i("path:" + path);
fileName = path + "user_head_photo.jpg";
tempFile = new File(fileName);
} else {
CommonUitl.toast(context, "请插入SD卡");
}
}
}
public void openCamera() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// 打开相机
intent.putExtra("output", Uri.fromFile(tempFile));
startActivityForResult(intent, OPEN_CAMERA_CODE);
}
public void openGallery() {
Intent intent = new Intent(Intent.ACTION_PICK);// 打开相册
intent.setDataAndType(MediaStore.Images.Media.INTERNAL_CONTENT_URI, "image
public void cropPhoto(Uri uri) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
intent.putExtra("output", Uri.fromFile(tempFile));
intent.putExtra("crop", true);
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", crop);
intent.putExtra("outputY", crop);
startActivityForResult(intent, CROP_PHOTO_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == 1)
return;
switch (requestCode) {
case OPEN_CAMERA_CODE:
cropPhoto(Uri.fromFile(tempFile));
break;
case OPEN_GALLERY_CODE:
cropPhoto(data.getData());
break;
case CROP_PHOTO_CODE:
try {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
Bitmap bitmap = BitmapFactory.decodeFile(fileName, options);
if (bitmap != null) {
iv_user_photo.setImageBitmap(bitmap);
CommonUitl.sharedPreferences(context, AppConstants.USER_PHOTO, fileName);
}
} catch (Exception e) {
e.printStackTrace();
}
break;
default:
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
以上就是关于Android相机、图册的基本操作内容,希望对大家学习Android软件编程有所帮助。
您可能感兴趣的文章:Android打开相机和相册实例代码Android实用控件自定义逼真相机光圈ViewAndroid实现IOS相机滑动控件Android实现用户头像更换操作Android仿微信群聊头像Android拍照或从图库选择图片并裁剪Android实现拍照、选择图片并裁剪图片功能解决Android从相册中获取图片出错图片却无法裁剪问题的方法Android裁剪图片为圆形图片的实现原理与代码Android实现相机拍摄、选择、图片裁剪功能