I'm trying to capture a picture from camera and simply print a log.
Here's my code:
public class CameraActivity extends AppCompatActivity {
private static final String TAG = "CameraActivity";
private static final int ACTIVITY_NUMBER = 2; //Used to identify the activity
private static final int PERMISSION_REQUEST_CODE = 1; //Used to request permission to the user
private Context context = CameraActivity.this;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
Log.d(TAG, "onCreate: started.");
//Check for permissions
if(Permissions.checkPermissionsArray(context, Permissions.PERMISSIONS_CAMERA)){
ActivityResultLauncher<Intent> activityResultLauncher = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
new ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult result) {
Log.d("dd", "ee");
}
}
);
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivity(cameraIntent);
}
else {
Permissions.requestPermissions(this, Permissions.PERMISSIONS_CAMERA, PERMISSION_REQUEST_CODE);
}
setupBottomNavigationView();
}
Now, the camera acquisition works just fine. I'm able to successfully take a picure, but no log message is shown. So I guess I'm doing the activityResult piece wrong.
What could be the reason?
CodePudding user response:
I found the error. I was using the method startActivity
instead of the activityResultLauncher.launch
one.
CodePudding user response:
Add on activity result after on activity.
public class CameraActivity extends AppCompatActivity {
private static final String TAG = "CameraActivity";
private static final int ACTIVITY_NUMBER = 2; //Used to identify the activity
private static final int PERMISSION_REQUEST_CODE = 1; //Used to request permission to the user
private Context context = CameraActivity.this;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
Log.d(TAG, "onCreate: started.");
//Check for permissions
if(Permissions.checkPermissionsArray(context, Permissions.PERMISSIONS_CAMERA)){
ActivityResultLauncher<Intent> activityResultLauncher = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
new ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult result) {
Log.d("dd", "ee");
}
}
);
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent , ACTIVITY_NUMBER );
}
else {
Permissions.requestPermissions(this, Permissions.PERMISSIONS_CAMERA, PERMISSION_REQUEST_CODE);
}
setupBottomNavigationView();
}
and then add on Activity result.
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case TAKE_PICTURE:
if (resultCode == Activity.RESULT_OK) {
Uri selectedImage = imageUri;
Log.d("dd", selectedImage.toString );
}
}
}