[英]How to encapsulate a startActivityForResult in a Java class and get the onActivityResult on the Activity that called the method
Im using the next camera code in several activities and I would like to make a class that encapsulate the methods for using the camera in Android. 我在几个活动中使用下一个摄像头代码,我想创建一个类,用于封装在Android中使用摄像头的方法。
What I am trying to get is the Activity class be something like: 我想要得到的是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
}
The Camera class looks something like this: 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);
}
}
}
}
The issue that I have right now is that I never get a call back from onActivityResult
from the fragment. 我现在遇到的问题是我从来没有从片段中获取
onActivityResult
回调。
OS has no support to send onActivityResult()
to Fragment
's. 操作系统不支持将
onActivityResult()
发送到Fragment
的。 Support library, though, has a mechanism to do that, that registers the call to a special table in the AppCompatActivity
. 但是,支持库有一种机制可以将调用注册到
AppCompatActivity
的特殊表。 The trick here is that you have to use Fragment
's own startActivityForResult()
, not Activity
's one. 这里的诀窍是你必须使用
Fragment
自己的startActivityForResult()
,而不是Activity
的。
So, your Camera
class code should look like: 因此,您的
Camera
类代码应如下所示:
public class Camera{
public static void dispatchTakePictureIntent(Activity activity, Fragment fragment, 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);
fragment.startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
}
}
Notice last line is using Fragment
's startActivityForResult()
注意最后一行是使用
Fragment
的startActivityForResult()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.