【发布时间】:2019-08-23 03:32:35
【问题描述】:
我在几个活动中使用下一个相机代码,我想创建一个类来封装在 Android 中使用相机的方法。
我想要的是 Activity 类是这样的:
Public class Myfragment extends Fragment{
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.profile_fragment, container, false);
mButtonProfilePhoto = v.findViewById(R.id.buttonProfilePhoto);
mButtonProfilePhoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Here I call the camera intent.
Camera.dispatchTakePictureIntent(getActivity(), mPhotoFile);
}
});
return v;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
//handle the camera result
}
Camera 类看起来像这样:
public class Camera{
public static void dispatchTakePictureIntent(Activity activity, File file) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(activity.getPackageManager()) != null) {
// Create the File where the photo should go
file = null;
try {
file = Camera.createImageFile(activity);
} catch (IOException ex) {
// Error occurred while creating the File
}
// Continue only if the File was successfully created
if (file != null) {
Uri photoURI = FileProvider.getUriForFile(activity,
"com.itcom202.weroom.fileprovider",
file );
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult( takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
}
}
我现在遇到的问题是我从未收到来自片段的onActivityResult 的回电。
【问题讨论】:
-
嗨黛米,欢迎来到 S.O.您的意思是要在片段本身而不是在其父活动中“捕获”结果事件?
-
嗨 Barackos,我想从片段中读取 onactivityResult,但是相机的方法将在一个类中实现(不是活动或片段)。
标签: java android android-intent fragment onactivityresult