Guest User

Untitled

a guest
Jan 20th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.11 KB | None | 0 0
  1. String userChoosenTask;
  2.  
  3. /**Called when activity is first created. */
  4. @Override
  5. protected void onCreate(Bundle savedInstanceState) {
  6. super.onCreate(savedInstanceState);
  7. setContentView(R.layout.activity_main);
  8.  
  9. Button button = findViewById(R.id.btnSelectPhoto);
  10. button.setOnClickListener(new View.OnClickListener() {
  11. @Override
  12. public void onClick(View view) {
  13. selectImage(view);
  14. }
  15. });
  16. }
  17.  
  18.  
  19. //Method for selecting image from camera or gallery
  20. private void selectImage(View view){
  21. final CharSequence[] items = {"Take Photo", "Choose from Library","Cancel"};
  22. AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
  23. builder.setTitle("Add Photo!");
  24. builder.setItems(items, new DialogInterface.OnClickListener() {
  25. @Override
  26. public void onClick(DialogInterface dialogInterface, int i) {
  27. boolean result = Utility.checkPermission(MainActivity.this);
  28. if (items[i].equals("Take Photo")) {
  29. userChoosenTask = "Take Photo";
  30. if (result) {
  31. cameraIntent();
  32. }
  33. }
  34. else if (items[i].equals("Choose from library")){
  35. userChoosenTask = "Choose from library";
  36. if(result)
  37. galleryIntent();
  38. }
  39. else if(items[i].equals("Cancel")){
  40. dialogInterface.dismiss();
  41. }
  42. }
  43. });
  44. builder.show();
  45. }
  46.  
  47. //Gallery Intent Method
  48. public static final int SELECT_FILE =2;
  49. private void galleryIntent()
  50. {
  51. Intent intent = new Intent();
  52. intent.setType("image/*");
  53. intent.setAction(Intent.ACTION_GET_CONTENT);
  54. startActivityForResult(Intent.createChooser(intent, "Select File"), SELECT_FILE);
  55. }
  56.  
  57.  
  58. // Camera Intent Method
  59. public static final int REQUEST_IMAGE_CAPTURE =1;
  60. private void cameraIntent()
  61. {
  62. Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  63. startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
  64. }
  65.  
  66. //Requesting Permission at run time
  67. public class Utility {
  68. public static final int MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE = 123;
  69.  
  70. public static boolean checkPermission(final Context context) {
  71.  
  72. int currentAPIVersion = Build.VERSION.SDK_INT;
  73. if (currentAPIVersion >= Build.VERSION_CODES.M) {
  74. if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
  75. if (ActivityCompat.shouldShowRequestPermissionRationale((Activity) context, Manifest.permission.READ_EXTERNAL_STORAGE)) {
  76. AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context);
  77. alertBuilder.setCancelable(true);
  78. alertBuilder.setTitle("Permission necessary");
  79. alertBuilder.setMessage("External storage permission is necessary");
  80. alertBuilder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
  81. @Override
  82. public void onClick(DialogInterface dialogInterface, int i) {
  83. ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);
  84. }
  85. });
  86. AlertDialog alert = alertBuilder.create();
  87. alert.show();
  88. } else {
  89. ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);
  90. }
  91. return false;
  92. } else {
  93. return true;
  94. }
  95. } else {
  96. return true;
  97. }
  98. }
  99.  
  100. //Gallery Intent Method
  101. public static final int SELECT_FILE =2;
  102. private void galleryIntent()
  103. {
  104. Intent intent = new Intent();
  105. intent.setType("image/*");
  106. intent.setAction(Intent.ACTION_GET_CONTENT);
  107. startActivityForResult(Intent.createChooser(intent, "Select File"), SELECT_FILE);
  108. }
  109.  
  110. }
  111. break;
  112. }
Add Comment
Please, Sign In to add comment