Here is my code: Here Is how i select image from gallery or capture it. Please tell me How i can save it.
private void selectImage() {
final CharSequence[] items = { "Take Photo", "Choose from Library", "Cancel" };
AlertDialog.Builder builder = new AlertDialog.Builder(Enrolement.this);
builder.setTitle("Add Photo!");
builder.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
if (items[item].equals("Take Photo")) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUEST_CAMERA);
} else if (items[item].equals("Choose from Library")) {
Intent intent = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(
Intent.createChooser(intent, "Select File"),
SELECT_FILE);
} else if (items[item].equals("Cancel")) {
dialog.dismiss();
}
}
});
builder.show();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == SELECT_FILE)
onSelectFromGalleryResult(data);
else if (requestCode == REQUEST_CAMERA)
onCaptureImageResult(data);
}
}
For Capture
private void onCaptureImageResult(Intent data) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
File destination = new File(Environment.getExternalStorageDirectory(),
System.currentTimeMillis() + ".jpg");
FileOutputStream fo;
try {
destination.createNewFile();
fo = new FileOutputStream(destination);
fo.write(bytes.toByteArray());
fo.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
ivImage.setImageBitmap(thumbnail);
}
For Gallery i am using this
@SuppressWarnings("deprecation")
private void onSelectFromGalleryResult(Intent data) {
Uri selectedImageUri = data.getData();
String[] projection = { MediaStore.MediaColumns.DATA };
Cursor cursor = managedQuery(selectedImageUri, projection, null, null,
null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
cursor.moveToFirst();
String selectedImagePath = cursor.getString(column_index);
Bitmap bm;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(selectedImagePath, options);
final int REQUIRED_SIZE = 200;
int scale = 1;
while (options.outWidth / scale / 2 >= REQUIRED_SIZE
&& options.outHeight / scale / 2 >= REQUIRED_SIZE)
scale *= 2;
options.inSampleSize = scale;
options.inJustDecodeBounds = false;
bm = BitmapFactory.decodeFile(selectedImagePath, options);
ivImage.setImageBitmap(bm);
}
after This i want to retrieve Image in a List View. This time i am able to fetch only other info except image. Thanks
My DataBase class extending SQLiteOpenHelper
public class MyDBHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "employee.db";
public static final String TABLE_EMPLOYEE = "employeeData";
public static final String COLUMN_ID = "id";
public static final String COLUMN_NAME = "name";
public static final String COLUMN_DOB = "dob";
public static final String COLUMN_ADDRESS = "address";
public static final String COLUMN_OCCUPATION = "Occupation";
//public static final String COLUMN_IMAGE_PATH = "image_path";
public static final String KEY_IMAGE = "image_data";
public MyDBHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("DROP TABLE IF EXISTS TABLE_EMPLOYEE");
String query = "CREATE TABLE " + TABLE_EMPLOYEE + " ( " +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT , " +
COLUMN_NAME + " TEXT ," +
COLUMN_DOB + " TEXT ," +
COLUMN_ADDRESS + " TEXT ," +
KEY_IMAGE + "BLOB ," +
// COLUMN_IMAGE_PATH + " TEXT ," +
COLUMN_OCCUPATION + " TEXT " +
");";
db.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS" + TABLE_EMPLOYEE);
onCreate(db);
}
// Add a new row to the databse
public boolean addemplyee(Employee employee) {
ContentValues values = new ContentValues();
//values.put(COLUMN_ID, employee.get_id());
values.put(COLUMN_NAME, employee.get_name());
values.put(COLUMN_DOB, employee.get_dob());
values.put(COLUMN_ADDRESS, employee.get_address());
values.put(COLUMN_OCCUPATION, employee.get_occupation());
values.put(KEY_IMAGE, employee.get_image());
//values.put(COLUMN_IMAGE_PATH, employee.get_occupation());
SQLiteDatabase db = getWritableDatabase();
Log.i("", "isinsert===" + db.insert(TABLE_EMPLOYEE, null, values));
db.close();
return true;
}
public Cursor getAllEmployees() {
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery("SELECT * FROM " + TABLE_EMPLOYEE, null);
return res;
}
}
Please check this one ::