Created
September 7, 2017 15:03
-
-
Save chnouman/d6ffa8805373311dc0d7333e731b62e0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void take(){ | |
Intent callCameraApplicationIntent = new Intent(); | |
callCameraApplicationIntent.setAction(MediaStore.ACTION_IMAGE_CAPTURE); | |
File photoFile = null; | |
try { | |
photoFile = createImageFile(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
String authorities | |
= getApplicationContext().getPackageName()+".fileprovider"; | |
Uri imageUri = FileProvider.getUriForFile(this,authorities,photoFile); | |
callCameraApplicationIntent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri); | |
startActivityForResult(callCameraApplicationIntent, ACTIVITY_START_CAMERA_APP); | |
} | |
protected void onActivityResult (int requestCode, int resultCode, Intent data) { | |
if(requestCode == ACTIVITY_START_CAMERA_APP && resultCode == RESULT_OK) { | |
// Toast.makeText(this, "Picture taken successfully", Toast.LENGTH_SHORT).show(); | |
// Bundle extras = data.getExtras(); | |
// Bitmap photoCapturedBitmap = (Bitmap) extras.get("data"); | |
// ivImage.setImageBitmap(photoCapturedBitmap); | |
// Bitmap photoCapturedBitmap = BitmapFactory.decodeFile(mImageFileLocation); | |
// ivImage.setImageBitmap(photoCapturedBitmap); | |
setReducedImageSize(); | |
} | |
} | |
File createImageFile() throws IOException { | |
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); | |
String imageFileName = "IMAGE_" + timeStamp + "_"; | |
File storageDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); | |
File image = File.createTempFile(imageFileName,".jpg", storageDirectory); | |
mImageFileLocation = image.getAbsolutePath(); | |
phtoUri = getImageContentUri(ReportActivity.this,image); | |
return image; | |
} | |
public static Uri getImageContentUri(Context context, File imageFile) { | |
String filePath = imageFile.getAbsolutePath(); | |
Cursor cursor = context.getContentResolver().query( | |
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, | |
new String[] { MediaStore.Images.Media._ID }, | |
MediaStore.Images.Media.DATA + "=? ", | |
new String[] { filePath }, null); | |
if (cursor != null && cursor.moveToFirst()) { | |
int id = cursor.getInt(cursor.getColumnIndex(MediaStore.MediaColumns._ID)); | |
cursor.close(); | |
return Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "" + id); | |
} else { | |
if (imageFile.exists()) { | |
ContentValues values = new ContentValues(); | |
values.put(MediaStore.Images.Media.DATA, filePath); | |
return context.getContentResolver().insert( | |
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); | |
} else { | |
return null; | |
} | |
} | |
} | |
void setReducedImageSize() { | |
int targetImageViewWidth = ivImage.getWidth(); | |
int targetImageViewHeight = ivImage.getHeight(); | |
BitmapFactory.Options bmOptions = new BitmapFactory.Options(); | |
bmOptions.inJustDecodeBounds = true; | |
BitmapFactory.decodeFile(mImageFileLocation, bmOptions); | |
int cameraImageWidth = bmOptions.outWidth; | |
int cameraImageHeight = bmOptions.outHeight; | |
int scaleFactor = Math.min(cameraImageWidth / targetImageViewWidth, cameraImageHeight / targetImageViewHeight); | |
bmOptions.inSampleSize = scaleFactor; | |
bmOptions.inJustDecodeBounds = false; | |
Bitmap photoReducedSizeBitmp = BitmapFactory.decodeFile(mImageFileLocation, bmOptions); | |
ivImage.setImageBitmap(photoReducedSizeBitmp); | |
photo = photoReducedSizeBitmp; | |
if (photo != null) { | |
//photo = BitmapHelper.shrinkBitmap(photo, 300, rotateXDegrees); | |
ImageView imageView = (ImageView) findViewById(R.id.ivImage); | |
Glide.with(ReportActivity.this).load(mImageFileLocation).override(100, 100).into(ivImage); | |
// imageView.setImageBitmap(photo); | |
photoPaths = mImageFileLocation; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment