試試這個
private void selectImage() {
final CharSequence[] options = { "Take Photo", "Choose from Gallery","Cancel" };
AlertDialog.Builder builder = new AlertDialog.Builder(ProfileSettings.this);
builder.setTitle("Profile Picture!");
builder.setItems(options, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
if (options[item].equals("Take Photo")) {
Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePicture, TAKEPIC);
} else if (options[item].equals("Choose from Gallery")) {
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, PICKPIC);
} else if (options[item].equals("Cancel")) {
dialog.dismiss();
}
}
});
builder.show();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==RESULT_OK)
{
if(requestCode==TAKEPIC)
{
Bitmap bitmap=(Bitmap) data.getExtras().get("data");
Uri selectedImage =getImageUri(YOURACTIVITY.this,bitmap);
String localimagepath =getImagePath(YOURACTIVITY.this, selectedImage);
}
else if(requestCode==PICKPIC)
{
Uri selectedImage = data.getData();
String localimagepath = getImagePath(YOURACTIVITY.this, data.getData());
}
}
}
public Uri getImageUri(Context inContext, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage,"title",null);
return Uri.parse(path);
}
public String getImagePath(Context context,Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor =context.getContentResolver().query(uri,projection,null,null,null); //managedQuery(uri, projection, null, null, null);
if(cursor!=null) {
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
else return null;
}
public void saveImagePath(String imagePath)
{
//push image path to db
DBHelpher dbHelpher=new DBHelpher(YOURACTIVITY.this);
dbHelpher.insertImagePath(imagePath);
}
創建類的源碼保存在數據庫中的圖像路徑
public class DBHelpher extends SQLiteOpenHelper
{
Context context;
public final String TABLE_SAMPLE="test";
public final String COLUMN_ID="id";
public final String COLUMN_IMAGEPATH="imagepath";
public final String CREATE_TABLE__SAMPLE_SQL="create table " + TABLE_SAMPLE
+ " (" + COLUMN_ID + " integer primary key autoincrement, "
+ COLUMN_IMAGEPATH + " text "
+ ");";
public DBHelpher(Context context)
{
super(context, "DATABASENAME.db", null, 1);
this.context=context;
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE__SAMPLE_SQL);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public void insertImagePath(String imagePath)
{
SQLiteDatabase database = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_IMAGEPATH,imagePath);
database.insert(TABLE_SAMPLE, null, values);
}
}
從您想要拍照的地方調用selectImage方法。