I have an activity wherin I give the user an option to click an image from the camera, then I store this image in a byte array and in the Database. However my code does not
Above ans with some correction for normal samsung device and orientaton issue problem
private void cameraIntent() {
new AlertDialog.Builder(this).setItems(B,new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialoginterface,int i) {
if (i == 0) {
String manufacturer_Type = android.os.Build.MANUFACTURER;
if(manufacturer_Type.equalsIgnoreCase("samsung")) {
Toast.makeText(getApplicationContext(), "Device man"+manufacturer_Type, Toast.LENGTH_LONG).show();
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA_IMAGE_CAPTURE);
} else {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
}
} else if (i == 1) {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, RESULT_LOAD_IMAGE);
}
}
}).show();
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==CAMERA_IMAGE_CAPTURE && resultCode==Activity.RESULT_OK){
// Describe the columns you'd like to have returned. Selecting from the Thumbnails location gives you both the Thumbnail Image ID, as well as the original image ID
String[] projection = {
MediaStore.Images.Thumbnails._ID, // The columns we want
MediaStore.Images.Thumbnails.IMAGE_ID,
MediaStore.Images.Thumbnails.KIND,
MediaStore.Images.Thumbnails.DATA};
String selection = MediaStore.Images.Thumbnails.KIND + "=" + // Select only mini's
MediaStore.Images.Thumbnails.MINI_KIND;
String sort = MediaStore.Images.Thumbnails._ID + " DESC";
//At the moment, this is a bit of a hack, as I'm returning ALL images, and just taking the latest one. There is a better way to narrow this down I think with a WHERE clause which is currently the selection variable
myCursor = CameraActivity.this.managedQuery(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, projection, selection, null, sort);
long imageId = 0l;
long thumbnailImageId = 0l;
String thumbnailPath = "";
try {
int cursor_size = myCursor.getCount();
if(cursor_size>0){
myCursor.moveToFirst();
imageId = myCursor.getLong(myCursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails.IMAGE_ID));
thumbnailImageId = myCursor.getLong(myCursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID));
thumbnailPath = myCursor.getString(myCursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails.DATA));
myCursor.close();
//Create new Cursor to obtain the file Path for the large image
String[] largeFileProjection = {
MediaStore.Images.ImageColumns._ID,
MediaStore.Images.ImageColumns.DATA
};
String largeFileSort = MediaStore.Images.ImageColumns._ID + " DESC";
Cursor mCursor = CameraActivity.this.managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, largeFileProjection, null, null, largeFileSort);
largeImagePath = "";
try {
mCursor.moveToFirst();
//This will actually give you the file path location of the image.
largeImagePath = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Images.ImageColumns.DATA));
}finally{
mCursor.close();
}
// These are the two URI's you'll be interested in. They give you a handle to the actual images
uriLargeImage = Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, String.valueOf(imageId));
uriThumbnailImage = Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, String.valueOf(thumbnailImageId));
// I've left out the remaining code, as all I do is assign the URI's to my own objects anyways...
// Toast.makeText(this, ""+largeImagePath, Toast.LENGTH_LONG).show();
// Toast.makeText(this, ""+uriLargeImage, Toast.LENGTH_LONG).show();
// Toast.makeText(this, ""+uriThumbnailImage, Toast.LENGTH_LONG).show();
if (largeImagePath != null) {
Toast.makeText(this, "LARGE YES"+largeImagePath, Toast.LENGTH_LONG).show();
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inSampleSize = OG;
thumbnail = BitmapFactory.decodeFile((largeImagePath), opts);
System.gc();
if (thumbnail != null) {
Toast.makeText(this, "Try Without Saved Instance", Toast.LENGTH_LONG).show();
Bitmap rotatedImage = exifImage(thumbnail, largeImagePath);
// imageCam(thumbnail);
if(imageview!=null){
imageview.setImageBitmap(rotatedImage);
}
}
}
if (uriLargeImage != null) {
Toast.makeText(this, ""+uriLargeImage, Toast.LENGTH_LONG).show();
}
if (uriThumbnailImage != null) {
Toast.makeText(this, ""+uriThumbnailImage, Toast.LENGTH_LONG).show();
}
}else{
requestCode=CAMERA_SAMSUNG_NORMAL_PIC_REQUEST;
normalDeviceCamera(requestCode, resultCode, data);
}
} catch(Exception e){
e.printStackTrace();
}finally {
if(!myCursor.isClosed()){
myCursor.close();
}
}
}
if( requestCode == CAMERA_PIC_REQUEST && resultCode== RESULT_OK){
normalDeviceCamera(requestCode, resultCode, data);
}
}
private void normalDeviceCamera(int requestCode, int resultCode, Intent data){
if( requestCode == CAMERA_PIC_REQUEST && resultCode== RESULT_OK){
Bundle extras = data.getExtras();
if (extras.keySet().contain`enter code here`s("data") ){
BitmapFactory.Options options = new BitmapFactory.Options();
thumbnail = (Bitmap) extras.get("data");
if (thumbnail != null) {
Toast.makeText(this, "YES Thumbnail", Toast.LENGTH_LONG).show();
/* BitmapFactory.Options opt = new BitmapFactory.Options();*/
thumbnail = (Bitmap) extras.get("data");
if(imageview!=null){
imageview.setImageBitmap(thumbnail);
}
}
} else {
Uri imageURI = getIntent().getData();
// ImageView imageview = (ImageView)findViewById(R.id.imgCam);
if(imageview!=null){
imageview.setImageURI(imageURI);
}
if(imageURI != null){
Toast.makeText(this, "YES Image Uri", Toast.LENGTH_LONG).show();
}
}
super.onActivityResult(requestCode, resultCode, data);
}
}