我写了一个代码,用户可以拍照或从他的库中选择。 我曾打算打开画廊/相机,它从画廊返回位图或文件的路径。我收到的数据尝试将其保存在接口上,但由于某种原因,它总是崩溃,因为它为空。
如果接口实际获得位图/文件路径,我不明白该接口如何变为空。
代码
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);
}
}
我运行该应用程序的设备是小米A1-我不知道它是否相关。
编辑
我已经通过在“ MainActivity”中实现接口解决了问题,并调用了该接口的func并传递了数据,它就起作用了!
//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);
}
那就是我要做的
答案 0 :(得分:1)
您的MainActivity(所有片段所依赖的活动)需要实现接口,您需要在Fragment
现在的问题是如何将数据从
Activity
传输到Fragment
,为此,您需要使用Fragment的findFragmentById
方法,看一看示例我已将数据从Activity
传输到Fragment
public class MainActivity extends AppCompatActivity implements OnPhotoReceivedListener {
@Override
public void getImagePath(String name) {
YOUR_FRAGMENT fragment = (YOUR_FRAGMENT) getFragmentManager().findFragmentById(R.id.fragment_container);
fragment.setDataForInterface(name); //// THESE METHOD , YOU NEED TO CREATE ON THE FRAGMENT
}
}
在onCreateView
内,您需要像下面这样初始化OnPhotoReceivedListener
接口
OnPhotoReceivedListener mOnPhotoReceived;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
/**
* This is the main error which u are doing
*/
try {
mOnPhotoReceived = (OnPhotoReceivedListener ) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement MyInterface ");
}
}
您需要创建一种方法,该方法将从父Activity
到Fragment
的数据中获取数据
public void setDataForInterface(String yourData) {
/**
* THIS IS YOUR DATA WHICH IS COME FROM THE PARENT ACTIVITY
*/
}