Android-存储活动结果返回null
我写了一个代码,用户可以拍照或从他的图书馆中选择。 我使用intent打开gallery/camera,它返回位图或gallery中文件的路径。我试图将收到的数据保存到接口上,但由于某些原因,它总是崩溃,因为它的值为null 我不明白如果接口实际得到位图/文件路径,它怎么会变成null 代码Android-存储活动结果返回null,android,Android,我写了一个代码,用户可以拍照或从他的图书馆中选择。 我使用intent打开gallery/camera,它返回位图或gallery中文件的路径。我试图将收到的数据保存到接口上,但由于某些原因,它总是崩溃,因为它的值为null 我不明白如果接口实际得到位图/文件路径,它怎么会变成null 代码 public class ChangeProfileImgDialog extends DialogFragment { private static final String TAG = "C
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);
}
这就是我所需要做的您的主要活动(所有片段所依赖的活动)需要实现在片段中使用的接口
现在的问题是如何将数据从Activity
传输到Fragment
,为此,需要使用片段的findFragmentById
方法,看看我将数据从Activity
传输到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 ");
}
}
您需要创建一个方法,该方法将从父级活动
到片段
public void setDataForInterface(String yourData) {
/**
* THIS IS YOUR DATA WHICH IS COME FROM THE PARENT ACTIVITY
*/
}
我添加了那行代码,但它使我的应用程序崩溃。实际上,我在“onAttach”@E.Bolandian中使用了这行代码。请检查我的更新解决方案。我已经在onAttach()中添加了代码,请检查solution@Overridepublic void onAttach(上下文上下文){super.onAttach(上下文);尝试{mOnPhotoReceived=(onphotoreceivedstener)getActivity();}catch(ClassCastException e){Log.e(标记,“onAttach:ClassCastException”,e.getCause();}}crash@E.Bolandian看一看解决方案…我的一个问题是,为什么需要interace来保存图像路径/位图?我想我已经找到了问题,但我不知道如何解决它。应用程序崩溃,因为mOnPhotoReceived为空-我不知道,如果我在“onAttach”中初始化它,是否已解决问题..请参考下面更新的解决方案。。。
public void setDataForInterface(String yourData) {
/**
* THIS IS YOUR DATA WHICH IS COME FROM THE PARENT ACTIVITY
*/
}