I wrote a code that the user can take a picture or choose from his libary. I used intent to open the gallery/camera, it returns the bitmap or the path of the file from the gallery. The data that i received i try to save it on interface but for some reason it always crash because its get null.
I dont understand how the interface gets null if it actually get the bitmap/file path.
CODE
public class ChangeProfileImgDialog extends DialogFragment {
private static final String TAG = "ChangePhotoDialog";
public static final int CAMERA_REQUEST_CODE = 1;//random number
public static final int PICKFILE_REQUEST_CODE = 0;//random number
public ChangeProfileImgDialog() {
// Required empty public constructor
}
public interface OnPhotoReceivedListener{
public void getImagePath(Uri imagePath);
public void getImageBitmap(Bitmap bitmap);
}
OnPhotoReceivedListener mOnPhotoReceived;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_change_profile_img_dialog, container, false);
//Initialize the textview for choosing an image from memory
TextView selectPhoto = (TextView) v.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.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, PICKFILE_REQUEST_CODE);
}
});
//Initialize the textview for choosing an image from memory
TextView takePhoto = (TextView) v.findViewById(R.id.dialogOpenCamera);
takePhoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "onClick: starting camera");
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST_CODE);
}
});
return v;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
/*
Results when selecting new image from phone memory
*/
if(requestCode == PICKFILE_REQUEST_CODE && resultCode == Activity.RESULT_OK){
Uri selectedImageUri = data.getData();
Log.d(TAG, "onActivityResult: image: " + selectedImageUri);
//send the uri and fragment to the interface
mOnPhotoReceived.getImagePath(selectedImageUri);
getDialog().dismiss();
}
else if(requestCode == CAMERA_REQUEST_CODE && resultCode == Activity.RESULT_OK){
Log.d(TAG, "onActivityResult: done taking a photo.");
Bitmap bitmap;
bitmap = (Bitmap) data.getExtras().get("data");
//send the bitmap and fragment to the interface
mOnPhotoReceived.getImageBitmap(bitmap);
getDialog().dismiss();
}
}
@Override
public void onAttach(Context context) {
try{
mOnPhotoReceived = (OnPhotoReceivedListener) getActivity();
}catch (ClassCastException e){
Log.e(TAG, "onAttach: ClassCastException", e.getCause() );
}
super.onAttach(context);
}
}
The device that i run the app is Xiaomi A1 - i dont know if it is related.
EDIT
I have resolve the problem by implement the interface in "MainActivity" and call the func of the interface and pass the data and it work!
//transfer the imagepath to MyAccountFragment
@Override
public void getImagePath(Uri imagePath) {
Log.d(TAG,imagePath.toString());
MyAccountFragment fragment = new MyAccountFragment();
fragment.getImagePath(imagePath);
}
//transfer the bitmap to MyAccountFragment
@Override
public void getImageBitmap(Bitmap bitmap) {
Log.d(TAG,bitmap.toString());
MyAccountFragment fragment = new MyAccountFragment();
fragment.getImageBitmap(bitmap);
}
Thats all i needed to do