Advertisement
Guest User

Untitled

a guest
Nov 17th, 2018
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.92 KB | None | 0 0
  1. package codingwithmitch.com.forsale;
  2.  
  3. import android.app.Activity;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.graphics.Bitmap;
  7. import android.net.Uri;
  8. import android.os.Bundle;
  9. import android.provider.MediaStore;
  10. import android.support.annotation.Nullable;
  11. import android.support.v4.app.DialogFragment;
  12. import android.util.Log;
  13. import android.view.LayoutInflater;
  14. import android.view.View;
  15. import android.view.ViewGroup;
  16. import android.widget.TextView;
  17.  
  18. /**
  19. * Created by User on 10/22/2017.
  20. */
  21.  
  22. public class SelectPhotoDialog extends DialogFragment{
  23.  
  24. private static final String TAG = "SelectPhotoDialog";
  25. private static final int PICKFILE_REQUEST_CODE = 1234;
  26. private static final int PICKFILE_REQUEST_CODE1 = 12345;
  27.  
  28. private static final int CAMERA_REQUEST_CODE = 4321;
  29. private static final int CAMERA_REQUEST_CODE1 = 43215;
  30.  
  31. public interface OnPhotoSelectedListener{
  32. void getImagePath(Uri imagePath);
  33. void getImagePath1(Uri imagePath1);
  34. void getImageBitmap(Bitmap bitmap);
  35. }
  36. OnPhotoSelectedListener mOnPhotoSelectedListener;
  37.  
  38. @Nullable
  39. @Override
  40. public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  41. View view = inflater.inflate(R.layout.dialog_selectphoto, container, false);
  42.  
  43. TextView selectPhoto = (TextView) view.findViewById(R.id.dialogChoosePhoto);
  44. selectPhoto.setOnClickListener(new View.OnClickListener() {
  45. @Override
  46. public void onClick(View v) {
  47. Log.d(TAG, "onClick: accessing phones memory.");
  48. Intent intent = new Intent();
  49. intent.setType("image/*");
  50. intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
  51. intent.setAction(Intent.ACTION_GET_CONTENT);
  52. startActivityForResult(intent, PICKFILE_REQUEST_CODE);
  53.  
  54. }
  55. });
  56.  
  57. TextView selectPhoto1 = (TextView) view.findViewById(R.id.dialogChoosePhoto1);
  58.  
  59. selectPhoto1.setOnClickListener(new View.OnClickListener() {
  60. @Override
  61. public void onClick(View v) {
  62. Log.d(TAG, "onClick: accessing phones memory.");
  63. Intent intent = new Intent();
  64. intent.setType("image/*");
  65. intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
  66. intent.setAction(Intent.ACTION_GET_CONTENT);
  67. startActivityForResult(intent, PICKFILE_REQUEST_CODE1);
  68. }
  69. });
  70.  
  71. TextView takePhoto = (TextView) view.findViewById(R.id.dialogOpenCamera);
  72. takePhoto.setOnClickListener(new View.OnClickListener() {
  73. @Override
  74. public void onClick(View v) {
  75. Log.d(TAG, "onClick: starting camera.");
  76. Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  77. startActivityForResult(intent, CAMERA_REQUEST_CODE);
  78.  
  79. }
  80. });
  81. return view;
  82. }
  83.  
  84. @Override
  85. public void onActivityResult(int requestCode, int resultCode, Intent data) {
  86. super.onActivityResult(requestCode, resultCode, data);
  87.  
  88. /*
  89. Results when selecting a new image from memory
  90. */
  91. if(requestCode == PICKFILE_REQUEST_CODE && resultCode == Activity.RESULT_OK){
  92. Uri mSelectedUri = data.getData();
  93. Log.d(TAG, "onActivityResult: image uri: " + mSelectedUri);
  94.  
  95. //send the uri to PostFragment & dismiss dialog
  96. mOnPhotoSelectedListener.getImagePath(mSelectedUri);
  97.  
  98. getDialog().dismiss();
  99. }
  100. /*
  101. Results when taking a new photo with camera
  102. */
  103. /* else if(requestCode == CAMERA_REQUEST_CODE && resultCode == Activity.RESULT_OK){
  104. Log.d(TAG, "onActivityResult: done taking new photo");
  105. Bitmap bitmap;
  106. bitmap = (Bitmap) data.getExtras().get("data");
  107.  
  108. //send the bitmap to PostFragment and dismiss dialog
  109. mOnPhotoSelectedListener.getImageBitmap(bitmap);
  110. getDialog().dismiss();
  111. }*/
  112.  
  113. //Second image
  114. if(requestCode == PICKFILE_REQUEST_CODE1 && resultCode == Activity.RESULT_OK){
  115.  
  116. Uri mSelectedUri1 = data.getData();
  117. Log.d(TAG, "onActivityResult: image uri: " + mSelectedUri1);
  118.  
  119. //send the uri to PostFragment & dismiss dialog
  120.  
  121. mOnPhotoSelectedListener.getImagePath1(mSelectedUri1);
  122. getDialog().dismiss();
  123. }
  124. }
  125.  
  126. @Override
  127. public void onAttach(Context context) {
  128. try{
  129. mOnPhotoSelectedListener = (OnPhotoSelectedListener) getTargetFragment();
  130. }catch (ClassCastException e){
  131. Log.e(TAG, "onAttach: ClassCastException: " + e.getMessage() );
  132. }
  133. super.onAttach(context);
  134. }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement