sorry i am a java newbie, i need to use camera from class extending LinearLayout.
public class ImagePicker extends LinearLayout {
...
mButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
try{
startActivityForResult(takePhotoIntent, REQUEST_TAKE_PHOTO);
}catch (ActivityNotFoundException e){
e.printStackTrace();
}
}
});
...
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
these methods need a class extending Activity. how can i use them correctly? Thanks
CodePudding user response:
There are (at least) two options to achieve this:
- you can get the activity from the ImagePicker as described in stackoverflow.com/a/32973351/3738870. However, this approach is less clean because the Intent is sent in
ImagePickerbut the result still has to be received inonActivityResultin theActivity. It's better to receive the result in the same class from where you requested it. - the other approach is exposing the button and then calling the
setOnClickListeneron it in the Activity. Here's some sample code that demonstrates how to do this:
ImagePicker:
public class ImagePicker extends LinearLayout {
private Button button;
public ImagePicker(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
View.inflate(context, R.layout.image_picker, this);
button = findViewById(R.id.button);
}
public Button getButton() {
return button;
}
}
Activity:
public class Activity extends AppCompatActivity {
private static int REQUEST_TAKE_PHOTO = 99;
@Override
public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
setContentView(R.layout.activity_main2);
ImagePicker imagePicker = findViewById(R.id.imagePicker);
imagePicker.getButton().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
try{
startActivityForResult(takePhotoIntent, REQUEST_TAKE_PHOTO);
}catch (ActivityNotFoundException e){
e.printStackTrace();
}
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
}
An even simpler solution is to just get the Button directly in the Activity:
public class Activity extends AppCompatActivity {
private static int REQUEST_TAKE_PHOTO = 99;
@Override
public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
setContentView(R.layout.activity_main2);
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
try{
startActivityForResult(takePhotoIntent, REQUEST_TAKE_PHOTO);
}catch (ActivityNotFoundException e){
e.printStackTrace();
}
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
}
