我写了一个代码,用户可以拍照或从他的库中选择。 我用 Intent 打开图库/相机,它返回位图或图库中文件的路径。我收到的数据尝试将其保存在界面上,但由于某种原因它总是崩溃,因为它为空。
我不明白如果接口(interface)实际上获取了位图/文件路径,它是如何变为空的。
代码
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”中实现接口(interface)并调用接口(interface)的函数并传递数据来解决问题!
//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);
}
这就是我需要做的一切
最佳答案
您的 MainActivity(所有 fragment 所依赖的 Activity)需要实现您需要在 Fragment
中使用的接口(interface)
Now the problem is that how you will transfer data from
Activity
toFragment
for which, you need to use thefindFragmentById
method of the Fragment, have a look on the example on which i have transferred the data fromActivity
toFragment
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
接口(interface)
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
*/
}
关于Android - 存储 Activity 结果返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52256762/