/*.....Added this into manifest file.....*/
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"></meta-data>
</provider>
/*.....Create an xml packege and add an xml fille provider_paths.xml add this code.....*/
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path path="." name="mediaimages" />
</paths>
/*.....Added this dependency in to gradle file.....*/
compile 'com.github.bumptech.glide:glide:3.8.0'
/*.....Added this in to your main activity.....*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnCapturePicture = (Button) findViewById(R.id.btnCapturePicture);
btnGalleryPicture = (Button) findViewById(R.id.btnGalleryPicture);
btnUploadPicture = (Button) findViewById(R.id.btnUploadPicture);
imgview_pic = (ImageView) findViewById(R.id.imgview_pic);
btnCapturePicture.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// capture Picture
ActivityCompat.requestPermissions(MainActivity3.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CAPTURE_IMAGE);
}
});
btnGalleryPicture.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//gallery Picture
ActivityCompat.requestPermissions(MainActivity3.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_PROFILE_IMAGE);
}
});
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//Log.v(TAG,"Permission: "+permissions[0]+ " was "+grantResults[0]+" requestCode "+String.valueOf(requestCode));
//resume tasks needing this permission
if (requestCode == REQUEST_PROFILE_IMAGE) {
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, REQUEST_PROFILE_IMAGE);
}
else if (requestCode == REQUEST_CAPTURE_IMAGE) {
//Toast.makeText(getApplicationContext(), "Okay.", Toast.LENGTH_SHORT).show();
dispatchTakePictureIntent();
}
} else {
Toast.makeText(getApplicationContext(), "Permission denied to read your External Storage!", Toast.LENGTH_SHORT).show();
}
return;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_PROFILE_IMAGE && resultCode == Activity.RESULT_OK) {
Uri selectedImage = data.getData();
File selectedFile = null;
if (Build.VERSION.SDK_INT >= 23) {
selectedFile = new File(getPath(selectedImage));
}else {
selectedFile = new File(selectedImage.getPath());
}
long image_size = selectedFile.length() / 1024; //size in KB
String filePath = selectedFile.getPath();
String file_extn = filePath.substring(filePath.lastIndexOf(".") + 1);
String file_name = filePath.substring(filePath.lastIndexOf("/") + 1);
mImageFileLocation = filePath;
Log.d(TAG, mImageFileLocation);
if (file_extn.equals("img") || file_extn.equals("jpg") || file_extn.equals("jpeg") || file_extn.equals("gif") || file_extn.equals("png")) {
Log.d("fileDeatails: ", filePath + "\n"+ file_extn + "\n" + file_name);
Log.d("file size:", String.valueOf(image_size));
imgview_pic.setImageURI(selectedImage);
} else {
//NOT IN REQUIRED FORMAT
}
}
else if (requestCode == REQUEST_CAPTURE_IMAGE && resultCode == RESULT_OK) {
Glide.with(this).load(mImageFileLocation).into(imgview_pic);
//setReducedImageSize();
}
}
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
}
// Continue only if the File was successfully created
if (Build.VERSION.SDK_INT >= 23) {
String authorities = getApplicationContext().getPackageName() + ".fileprovider";
Uri imageUri = FileProvider.getUriForFile(this, authorities, photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
}else {
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
}
startActivityForResult(takePictureIntent, REQUEST_CAPTURE_IMAGE);
}
}
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();
return image;
}
void setReducedImageSize() {
int targetImageViewWidth = imgview_pic.getWidth();
int targetImageViewHeight = imgview_pic.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);
imgview_pic.setImageBitmap(photoReducedSizeBitmp);
}
public String getPath(Uri uri) {
String[] projection = {MediaStore.MediaColumns.DATA};
Cursor cursor = managedQuery(uri, projection, null, null, null);
column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
cursor.moveToFirst();
imagePath = cursor.getString(column_index);
return cursor.getString(column_index);
}
Comments
Post a Comment