一、Activity中调用相机和相册选择照片
//设置RequestCode常量
private static final int PHOTO_REQUEST_GALLERY=5;
private static final int PHOTO_REQUEST_CAREMA=6;
private static final int PHOTO_REQUEST_CUT=7;
//数据库
private PictureDatabase pictureDatabase;
private ArrayList<Bitmap> bitmap_list;
1从相册中选择照片:
private void gallery(View view) {
// 激活系统图库,选择一张图片
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
// 开启一个带有返回值的Activity,请求码为PHOTO_REQUEST_GALLERY
startActivityForResult(intent, PHOTO_REQUEST_GALLERY);
}
2.从相机中获取照片:
public void camera(View view) {
// 激活相机
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
// 判断存储卡是否可以用,可用进行存储
if (hasCard) {
tempFile = new File(Environment.getExternalStorageDirectory(),
PHOTO_FILE_NAME);
// 从文件中创建uri
Uri uri = Uri.fromFile(tempFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
}
// 开启一个带有返回值的Activity,请求码为PHOTO_REQUEST_CAREMA
startActivityForResult(intent, PHOTO_REQUEST_CAREMA);
}
3.对图片进行剪切:
private void crop(Uri uri) {
// 裁剪图片意图
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
intent.putExtra("crop", "true");
// 裁剪框的比例,1:1
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
// 裁剪后输出图片的尺寸大小
intent.putExtra("outputX", 250);
intent.putExtra("outputY", 250);
intent.putExtra("outputFormat", "JPEG");// 图片格式
intent.putExtra("noFaceDetection", true);// 取消人脸识别
intent.putExtra("return-data", true);
// 开启一个带有返回值的Activity,请求码为PHOTO_REQUEST_CUT
startActivityForResult(intent, PHOTO_REQUEST_CUT);
}
最后在OnActivityResult()方法中:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode){
case PHOTO_REQUEST_CAREMA:
if(hasCard){
crop(Uri.fromFile(tempFile));
}else{
Toast.getToast(StudentAttributeSettingActivity.this,"未找到内存卡,无法存储");
}
break;
case PHOTO_REQUEST_CUT:
if(data != null) {
Bitmap bitmap = data.getParcelableExtra("data");//拿到剪切后的照片
pictureDatabase.savePhoto(bitmap);//存储到数据库
iv_setting_Icon.setImageBitmap(bitmap);//显示拿到的照片
}
try{
tempFile.delete();
}catch (Exception e){
e.printStackTrace();
}
break;
}
}
当你需要调用相册或者相机的时候,只要在你需要的位置调用上述相机和相册的方法即可 ,传参的View是你获得的view。
二、在Activity中从数据库获取图片:
在Activity的onCreate()或者onResume()方法中调用如下方法即可获得存储的照片:
private void getPicture() {
bitmap_list= pictureDatabase.getDrawable();
for(int i=0;i<bitmap_list.size();i++){
Bitmap bitmap=bitmap_list.get(i);
if(bitmap != null){
iv_setting_Icon.setImageBitmap(bitmap);
}else{
iv_setting_Icon.setImageResource(R.drawable.head);
}
}
}
三、数据库类的完整代码:
package com.beidougd.bdg.dateBase;
import android.content.ContentValues;
import android.content.Context;
import android.content.res.Resources;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.provider.BaseColumns;
import com.beidougd.bdga.R;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
/**
* Created by yr01 on 2016/4/13.
*/
public class PictureDatabase extends SQLiteOpenHelper {
//数据库的字段
public static class PictureColumns implements BaseColumns {
public static final String PICTURE = "picture";
}
private Context mContext;
//数据库名
private static final String DATABASE_NAME = "picture.db";
//数据库版本号
private static final int DATABASE_Version = 1;
//表名
private static final String TABLE_NAME = "picture";
//创建数据库
public PictureDatabase (Context context) {
super(context, DATABASE_NAME, null, DATABASE_Version);
this.mContext = context;
}
//创建表并初始化表
@Override
public void onCreate (SQLiteDatabase db) {
String sql = "Create table " + TABLE_NAME + "(" + BaseColumns._ID
+ " integer primary key autoincrement," + PictureColumns.PICTURE
+ " blob not null);";
db.execSQL(sql);
}
//将转换后的图片存入到数据库中
public void savePhoto (Bitmap bitmap ) {
// Drawable drawable = context.getResources().getDrawable(resources); 传入int资源时用
SQLiteDatabase db = getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(PictureColumns.PICTURE, getPicture(bitmap));
// cv.put(PictureColumns.PICTURE, getPicture(drawable)); 传入int资源时用
db.delete(TABLE_NAME,null,null);//每次设置头像之前清空数据库的数据
db.insert(TABLE_NAME, null, cv);
}
//将drawable转换成可以用来存储的byte[]类型
private byte[] getPicture(Bitmap bitmap) {
// if(drawable == null) {
// return null;
// }
if(bitmap == null) {
return null;
}
// BitmapDrawable bd = (BitmapDrawable) drawable;传入int资源时用
// Bitmap bitmap = bd.getBitmap();
ByteArrayOutputStream os = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
return os.toByteArray();
}
//从数据库读取照片
public ArrayList<Bitmap> getDrawable() {
SQLiteDatabase db = getReadableDatabase();
ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>();
//查询数据库
Cursor c = db.rawQuery("select * from picture", null);
//遍历数据
if(c != null && c.getCount() != 0) {
while(c.moveToNext()) {
//获取数据
byte[] b = c.getBlob(c.getColumnIndexOrThrow(PictureDatabase.PictureColumns.PICTURE));
//将获取的数据转换成drawable
Bitmap bitmap = BitmapFactory.decodeByteArray(b, 0, b.length, null);
// BitmapDrawable bitmapDrawable = new BitmapDrawable(bitmap);
// Drawable drawable = bitmapDrawable;
bitmaps.add(bitmap);
}
}
return bitmaps;
}
//更新数据库
@Override
public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) {
String sql = " DROP TABLE IF EXISTS " + TABLE_NAME;
db.execSQL(sql);
onCreate(db);
}
}
此类中因为我是用来存储每次设置头像的图片的,所以我在每次添加新图片时都先将数据库中存储的照片删除,然后放如新图片,这样的话在Activity中就不需要循环遍历获取数据库中存的图片了,只要拿到第一个就可以了,上述遍历是为了大家存储多个图片时取图片而写的,大家可根据自己的情况来进行修改。