Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package codingwithmitch.com.forsale;
- import android.app.Activity;
- import android.content.Context;
- import android.content.Intent;
- import android.graphics.Bitmap;
- import android.net.Uri;
- import android.os.Bundle;
- import android.provider.MediaStore;
- import android.support.annotation.Nullable;
- import android.support.v4.app.DialogFragment;
- import android.util.Log;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.TextView;
- /**
- * Created by User on 10/22/2017.
- */
- public class SelectPhotoDialog extends DialogFragment{
- private static final String TAG = "SelectPhotoDialog";
- private static final int PICKFILE_REQUEST_CODE = 1234;
- private static final int PICKFILE_REQUEST_CODE1 = 12345;
- private static final int CAMERA_REQUEST_CODE = 4321;
- private static final int CAMERA_REQUEST_CODE1 = 43215;
- public interface OnPhotoSelectedListener{
- void getImagePath(Uri imagePath);
- void getImagePath1(Uri imagePath1);
- void getImageBitmap(Bitmap bitmap);
- }
- OnPhotoSelectedListener mOnPhotoSelectedListener;
- @Nullable
- @Override
- public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
- View view = inflater.inflate(R.layout.dialog_selectphoto, container, false);
- TextView selectPhoto = (TextView) view.findViewById(R.id.dialogChoosePhoto);
- selectPhoto.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Log.d(TAG, "onClick: accessing phones memory.");
- Intent intent = new Intent();
- intent.setType("image/*");
- intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
- intent.setAction(Intent.ACTION_GET_CONTENT);
- startActivityForResult(intent, PICKFILE_REQUEST_CODE);
- }
- });
- TextView selectPhoto1 = (TextView) view.findViewById(R.id.dialogChoosePhoto1);
- selectPhoto1.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Log.d(TAG, "onClick: accessing phones memory.");
- Intent intent = new Intent();
- intent.setType("image/*");
- intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
- intent.setAction(Intent.ACTION_GET_CONTENT);
- startActivityForResult(intent, PICKFILE_REQUEST_CODE1);
- }
- });
- TextView takePhoto = (TextView) view.findViewById(R.id.dialogOpenCamera);
- takePhoto.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Log.d(TAG, "onClick: starting camera.");
- Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
- startActivityForResult(intent, CAMERA_REQUEST_CODE);
- }
- });
- return view;
- }
- @Override
- public void onActivityResult(int requestCode, int resultCode, Intent data) {
- super.onActivityResult(requestCode, resultCode, data);
- /*
- Results when selecting a new image from memory
- */
- if(requestCode == PICKFILE_REQUEST_CODE && resultCode == Activity.RESULT_OK){
- Uri mSelectedUri = data.getData();
- Log.d(TAG, "onActivityResult: image uri: " + mSelectedUri);
- //send the uri to PostFragment & dismiss dialog
- mOnPhotoSelectedListener.getImagePath(mSelectedUri);
- getDialog().dismiss();
- }
- /*
- Results when taking a new photo with camera
- */
- /* else if(requestCode == CAMERA_REQUEST_CODE && resultCode == Activity.RESULT_OK){
- Log.d(TAG, "onActivityResult: done taking new photo");
- Bitmap bitmap;
- bitmap = (Bitmap) data.getExtras().get("data");
- //send the bitmap to PostFragment and dismiss dialog
- mOnPhotoSelectedListener.getImageBitmap(bitmap);
- getDialog().dismiss();
- }*/
- //Second image
- if(requestCode == PICKFILE_REQUEST_CODE1 && resultCode == Activity.RESULT_OK){
- Uri mSelectedUri1 = data.getData();
- Log.d(TAG, "onActivityResult: image uri: " + mSelectedUri1);
- //send the uri to PostFragment & dismiss dialog
- mOnPhotoSelectedListener.getImagePath1(mSelectedUri1);
- getDialog().dismiss();
- }
- }
- @Override
- public void onAttach(Context context) {
- try{
- mOnPhotoSelectedListener = (OnPhotoSelectedListener) getTargetFragment();
- }catch (ClassCastException e){
- Log.e(TAG, "onAttach: ClassCastException: " + e.getMessage() );
- }
- super.onAttach(context);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement