Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // get prompts.xml view
- LayoutInflater layoutInflater = LayoutInflater.from(KidsProfileActivity.this);
- View promptView = layoutInflater.inflate(R.layout.image_choose_dialog, null);
- final AlertDialog.Builder choose_pop_up = new AlertDialog.Builder(KidsProfileActivity.this);
- choose_pop_up.setView(promptView);
- // create an alert dialog
- final AlertDialog alert = choose_pop_up.create();
- alert.show();
- TextView camera = (TextView) promptView.findViewById(R.id.camera_textview);
- TextView gallery = (TextView) promptView.findViewById(R.id.gallery_textview);
- camera.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- cameraIntent();
- alert.dismiss();
- }
- });
- gallery.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- galleryIntent();
- alert.dismiss();
- }
- });
- }
- /**** This method will call if user will choose camera ****/
- private void cameraIntent(){
- Intent CamIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
- File file_camera = new File(Environment.getExternalStorageDirectory(),
- "file" + String.valueOf(System.currentTimeMillis()) + ".jpeg");
- uri = Uri.fromFile(file_camera);
- CamIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, uri);
- CamIntent.putExtra("return-data", true);
- startActivityForResult(CamIntent, CAMERA_PIC_REQUEST);
- choose_button=1;
- }
- /**** This method will call if user will choose gallery ****/
- private void galleryIntent(){
- Intent galleryIntent = new Intent(
- Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
- File file_gallery = new File(Environment.getExternalStorageDirectory(),
- "file" + String.valueOf(System.currentTimeMillis()) + ".jpeg");
- uri = Uri.fromFile(file_gallery);
- galleryIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, uri);
- galleryIntent.putExtra("return-data", true);
- startActivityForResult(galleryIntent, RESULT_GALLERY_IMAGE);
- choose_button=2;
- }
- @Override
- public void onActivityResult(int requestCode, int resultCode, Intent data) {
- super.onActivityResult(requestCode, resultCode, data);
- try {
- switch(choose_button) {
- // ** This code for select the images from camera to set as profile image ** //
- case 1:
- // ** @support :- https://www.android-examples.com/android-image-cropping-example-tutorial-pick-gallery-camera/ ** //
- if (requestCode == CAMERA_PIC_REQUEST && resultCode == KidsProfileActivity.RESULT_OK) {
- cameraCropFunction();
- }
- else if (requestCode == 1) {
- if (data != null) {
- Bundle bundle = data.getExtras();
- assert bundle != null;
- Bitmap bitmap = bundle.getParcelable("data");
- kidProfile.setImageBitmap(bitmap);
- setProfileImage(bitmap);
- }
- }
- else {
- Toast.makeText(getApplicationContext(), "You have not picked the image", Toast.LENGTH_LONG).show();
- }
- break;
- // ** This code for select the images from gallery to set as profile image ** //
- case 2:
- if (requestCode == RESULT_GALLERY_IMAGE && resultCode == RESULT_OK && null != data) {
- // ** @support :- https://stackoverflow.com/questions/33229904/crop-image-from-gallery-and-set-it-as-my-imageview-background-android ** //
- Uri picUri = data.getData();
- String[] filePathColumn = {MediaStore.Images.Media.DATA};
- assert picUri != null;
- Cursor cursor = getContentResolver().query(picUri,
- filePathColumn, null, null, null);
- assert cursor != null;
- cursor.moveToFirst();
- int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
- String filePath = cursor.getString(columnIndex);
- cursor.close();
- galleryCropFunction(filePath);
- }
- else if (requestCode == 5 ) {
- if(resultCode == Activity.RESULT_OK){
- Bundle extras = data.getExtras();
- assert extras != null;
- Bitmap selectedBitmap = extras.getParcelable("data");
- kidProfile.setImageBitmap(selectedBitmap);
- setProfileImage(selectedBitmap);
- }
- }
- else {
- Toast.makeText(getApplicationContext(), "You haven't picked Image", Toast.LENGTH_LONG).show();
- }
- break;
- // **this code for select the images from gallery to set as wallpaper image ** //
- case 3 :
- if (requestCode == RESULT_WALLPAPER_IMAGE && resultCode == RESULT_OK
- && null != data) {
- Uri selected_Image = data.getData();
- String[] file_Path_Column = {MediaStore.Images.Media.DATA};
- assert selected_Image != null;
- Cursor cursor = getContentResolver().query(selected_Image,
- file_Path_Column, null, null, null);
- assert cursor != null;
- cursor.moveToFirst();
- int column_Index = cursor.getColumnIndex(file_Path_Column[0]);
- //String wallpaper_image = cursor.getString(column_Index);
- _wallImg= cursor.getString(column_Index);
- cursor.close();
- //kidsProfileUtills.setwallpaper(wallpaper_image);
- kidWallpaper.setImageBitmap(BitmapFactory
- .decodeFile(_wallImg));
- } else {
- Toast.makeText(getApplicationContext(), "You haven't picked Image",
- Toast.LENGTH_LONG).show();
- }
- break;
- }
- }catch (Exception e) {
- Toast.makeText(getApplicationContext(), "Something went wrong", Toast.LENGTH_LONG)
- .show();
- }
- }
- /** This method will call for crop the captured image from camera **/
- public void cameraCropFunction() {
- try {
- Intent CropIntent = new Intent("com.android.camera.action.CROP");
- CropIntent.setDataAndType(uri, "image/*");
- CropIntent.putExtra("crop", "true");
- CropIntent.putExtra("outputX", 180);
- CropIntent.putExtra("outputY", 180);
- CropIntent.putExtra("aspectX", 3);
- CropIntent.putExtra("aspectY", 4);
- CropIntent.putExtra("scaleUpIfNeeded", true);
- CropIntent.putExtra("return-data", true);
- startActivityForResult(CropIntent, 1);
- } catch (ActivityNotFoundException e) {e.printStackTrace();}
- }
- /** This method will call for crop the taken by gallery **/
- private void galleryCropFunction(String picPath) {
- try {
- Intent cropIntent = new Intent("com.android.camera.action.CROP");
- File f = new File(picPath);
- Uri contentUri = Uri.fromFile(f);
- cropIntent.setDataAndType(contentUri, "image/*");
- cropIntent.putExtra("crop", "true");
- cropIntent.putExtra("aspectX", 3);
- cropIntent.putExtra("aspectY", 4);
- cropIntent.putExtra("outputX", 180);
- cropIntent.putExtra("outputY", 180);
- cropIntent.putExtra("scaleUpIfNeeded", true);
- // retrieve data on return
- cropIntent.putExtra("return-data", true);
- // start the activity - we handle returning in onActivityResult
- startActivityForResult(cropIntent, 5);
- }
- catch (ActivityNotFoundException anfe) {
- String errorMessage = "your device doesn't support the crop action!";
- Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
- toast.show();
- }
- }
- /** This method is basically use to save the croped image into database **/
- public void setProfileImage(Bitmap imageBitmap){
- try {
- ByteArrayOutputStream bytes = new ByteArrayOutputStream();
- assert imageBitmap != null;
- imageBitmap.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
- String path = MediaStore.Images.Media.insertImage(getContentResolver(), imageBitmap, null, null);
- Uri uri = Uri.parse(path);
- Cursor cursor = getContentResolver().query(uri, null, null, null, null);
- assert cursor != null;
- cursor.moveToFirst();
- int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
- //String camera_image = cursor.getString(idx);
- _proImg = cursor.getString(idx); // This holding The User Profile Image
- cursor.close();
- //kidsProfileUtills.setimage(camera_image);
- }catch (Exception e) {
- e.printStackTrace();
- }
- }
Add Comment
Please, Sign In to add comment