Guest User

Untitled

a guest
Feb 21st, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.99 KB | None | 0 0
  1. // get prompts.xml view
  2. LayoutInflater layoutInflater = LayoutInflater.from(KidsProfileActivity.this);
  3. View promptView = layoutInflater.inflate(R.layout.image_choose_dialog, null);
  4. final AlertDialog.Builder choose_pop_up = new AlertDialog.Builder(KidsProfileActivity.this);
  5. choose_pop_up.setView(promptView);
  6.  
  7. // create an alert dialog
  8. final AlertDialog alert = choose_pop_up.create();
  9. alert.show();
  10.  
  11. TextView camera = (TextView) promptView.findViewById(R.id.camera_textview);
  12. TextView gallery = (TextView) promptView.findViewById(R.id.gallery_textview);
  13.  
  14. camera.setOnClickListener(new View.OnClickListener() {
  15. @Override
  16. public void onClick(View view) {
  17. cameraIntent();
  18. alert.dismiss();
  19. }
  20. });
  21. gallery.setOnClickListener(new View.OnClickListener() {
  22. @Override
  23. public void onClick(View view) {
  24. galleryIntent();
  25. alert.dismiss();
  26. }
  27. });
  28. }
  29.  
  30. /**** This method will call if user will choose camera ****/
  31. private void cameraIntent(){
  32. Intent CamIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
  33.  
  34. File file_camera = new File(Environment.getExternalStorageDirectory(),
  35. "file" + String.valueOf(System.currentTimeMillis()) + ".jpeg");
  36. uri = Uri.fromFile(file_camera);
  37.  
  38. CamIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, uri);
  39. CamIntent.putExtra("return-data", true);
  40. startActivityForResult(CamIntent, CAMERA_PIC_REQUEST);
  41.  
  42. choose_button=1;
  43. }
  44.  
  45. /**** This method will call if user will choose gallery ****/
  46. private void galleryIntent(){
  47. Intent galleryIntent = new Intent(
  48. Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
  49.  
  50. File file_gallery = new File(Environment.getExternalStorageDirectory(),
  51. "file" + String.valueOf(System.currentTimeMillis()) + ".jpeg");
  52. uri = Uri.fromFile(file_gallery);
  53.  
  54. galleryIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, uri);
  55. galleryIntent.putExtra("return-data", true);
  56. startActivityForResult(galleryIntent, RESULT_GALLERY_IMAGE);
  57.  
  58. choose_button=2;
  59. }
  60.  
  61. @Override
  62. public void onActivityResult(int requestCode, int resultCode, Intent data) {
  63. super.onActivityResult(requestCode, resultCode, data);
  64.  
  65. try {
  66. switch(choose_button) {
  67.  
  68. // ** This code for select the images from camera to set as profile image ** //
  69. case 1:
  70.  
  71. // ** @support :- https://www.android-examples.com/android-image-cropping-example-tutorial-pick-gallery-camera/ ** //
  72. if (requestCode == CAMERA_PIC_REQUEST && resultCode == KidsProfileActivity.RESULT_OK) {
  73. cameraCropFunction();
  74. }
  75. else if (requestCode == 1) {
  76.  
  77. if (data != null) {
  78. Bundle bundle = data.getExtras();
  79. assert bundle != null;
  80. Bitmap bitmap = bundle.getParcelable("data");
  81. kidProfile.setImageBitmap(bitmap);
  82.  
  83. setProfileImage(bitmap);
  84. }
  85. }
  86. else {
  87. Toast.makeText(getApplicationContext(), "You have not picked the image", Toast.LENGTH_LONG).show();
  88. }
  89. break;
  90.  
  91. // ** This code for select the images from gallery to set as profile image ** //
  92. case 2:
  93.  
  94. if (requestCode == RESULT_GALLERY_IMAGE && resultCode == RESULT_OK && null != data) {
  95.  
  96. // ** @support :- https://stackoverflow.com/questions/33229904/crop-image-from-gallery-and-set-it-as-my-imageview-background-android ** //
  97. Uri picUri = data.getData();
  98. String[] filePathColumn = {MediaStore.Images.Media.DATA};
  99. assert picUri != null;
  100. Cursor cursor = getContentResolver().query(picUri,
  101. filePathColumn, null, null, null);
  102. assert cursor != null;
  103. cursor.moveToFirst();
  104. int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
  105. String filePath = cursor.getString(columnIndex);
  106. cursor.close();
  107. galleryCropFunction(filePath);
  108.  
  109. }
  110. else if (requestCode == 5 ) {
  111.  
  112. if(resultCode == Activity.RESULT_OK){
  113.  
  114. Bundle extras = data.getExtras();
  115. assert extras != null;
  116. Bitmap selectedBitmap = extras.getParcelable("data");
  117. kidProfile.setImageBitmap(selectedBitmap);
  118.  
  119. setProfileImage(selectedBitmap);
  120. }
  121. }
  122. else {
  123. Toast.makeText(getApplicationContext(), "You haven't picked Image", Toast.LENGTH_LONG).show();
  124. }
  125. break;
  126.  
  127. // **this code for select the images from gallery to set as wallpaper image ** //
  128. case 3 :
  129. if (requestCode == RESULT_WALLPAPER_IMAGE && resultCode == RESULT_OK
  130. && null != data) {
  131.  
  132. Uri selected_Image = data.getData();
  133. String[] file_Path_Column = {MediaStore.Images.Media.DATA};
  134.  
  135. assert selected_Image != null;
  136. Cursor cursor = getContentResolver().query(selected_Image,
  137. file_Path_Column, null, null, null);
  138. assert cursor != null;
  139. cursor.moveToFirst();
  140.  
  141. int column_Index = cursor.getColumnIndex(file_Path_Column[0]);
  142. //String wallpaper_image = cursor.getString(column_Index);
  143. _wallImg= cursor.getString(column_Index);
  144. cursor.close();
  145.  
  146. //kidsProfileUtills.setwallpaper(wallpaper_image);
  147. kidWallpaper.setImageBitmap(BitmapFactory
  148. .decodeFile(_wallImg));
  149. } else {
  150. Toast.makeText(getApplicationContext(), "You haven't picked Image",
  151. Toast.LENGTH_LONG).show();
  152. }
  153. break;
  154. }
  155.  
  156. }catch (Exception e) {
  157. Toast.makeText(getApplicationContext(), "Something went wrong", Toast.LENGTH_LONG)
  158. .show();
  159. }
  160. }
  161.  
  162.  
  163. /** This method will call for crop the captured image from camera **/
  164. public void cameraCropFunction() {
  165.  
  166. try {
  167. Intent CropIntent = new Intent("com.android.camera.action.CROP");
  168.  
  169. CropIntent.setDataAndType(uri, "image/*");
  170.  
  171. CropIntent.putExtra("crop", "true");
  172. CropIntent.putExtra("outputX", 180);
  173. CropIntent.putExtra("outputY", 180);
  174. CropIntent.putExtra("aspectX", 3);
  175. CropIntent.putExtra("aspectY", 4);
  176. CropIntent.putExtra("scaleUpIfNeeded", true);
  177. CropIntent.putExtra("return-data", true);
  178.  
  179. startActivityForResult(CropIntent, 1);
  180.  
  181. } catch (ActivityNotFoundException e) {e.printStackTrace();}
  182. }
  183.  
  184. /** This method will call for crop the taken by gallery **/
  185. private void galleryCropFunction(String picPath) {
  186. try {
  187.  
  188. Intent cropIntent = new Intent("com.android.camera.action.CROP");
  189.  
  190. File f = new File(picPath);
  191. Uri contentUri = Uri.fromFile(f);
  192.  
  193. cropIntent.setDataAndType(contentUri, "image/*");
  194.  
  195. cropIntent.putExtra("crop", "true");
  196. cropIntent.putExtra("aspectX", 3);
  197. cropIntent.putExtra("aspectY", 4);
  198. cropIntent.putExtra("outputX", 180);
  199. cropIntent.putExtra("outputY", 180);
  200. cropIntent.putExtra("scaleUpIfNeeded", true);
  201. // retrieve data on return
  202. cropIntent.putExtra("return-data", true);
  203. // start the activity - we handle returning in onActivityResult
  204. startActivityForResult(cropIntent, 5);
  205. }
  206. catch (ActivityNotFoundException anfe) {
  207. String errorMessage = "your device doesn't support the crop action!";
  208. Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
  209. toast.show();
  210. }
  211. }
  212.  
  213. /** This method is basically use to save the croped image into database **/
  214. public void setProfileImage(Bitmap imageBitmap){
  215.  
  216. try {
  217. ByteArrayOutputStream bytes = new ByteArrayOutputStream();
  218. assert imageBitmap != null;
  219. imageBitmap.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
  220.  
  221. String path = MediaStore.Images.Media.insertImage(getContentResolver(), imageBitmap, null, null);
  222. Uri uri = Uri.parse(path);
  223.  
  224. Cursor cursor = getContentResolver().query(uri, null, null, null, null);
  225. assert cursor != null;
  226. cursor.moveToFirst();
  227.  
  228. int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
  229. //String camera_image = cursor.getString(idx);
  230. _proImg = cursor.getString(idx); // This holding The User Profile Image
  231. cursor.close();
  232.  
  233. //kidsProfileUtills.setimage(camera_image);
  234. }catch (Exception e) {
  235. e.printStackTrace();
  236. }
  237. }
Add Comment
Please, Sign In to add comment