I have 2 class: Main and Image.
My problem is how can I call a function from Image to Main because I have this error: Non-static method 'pickImg()' cannot be referenced from a static context
- Main = main program
- Image = getting image by camera capture/gallery and cropping
Main.java
public void getImg(){
ImageView iv1 = findViewById(R.id.imgVw);
Bitmap img = Image.pickImg();
iv1.setImageBitmap(img);
}
Image.java
public void pickImg() {
try {
Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(captureIntent, CAMERA_CAPTURE);
} catch (ActivityNotFoundException anfe) {
Toast toast = Toast.makeText(this, "This device doesn't support the crop action!", Toast.LENGTH_SHORT);
toast.show();
}
}
I read some topics about the error above and i tried to change public void pickImg()
to public static Bitmap pickImg(Bitmap img)
and put return
at the end to return the image to fulfill the error above but I get an error to startActivityForResult
. I also tried using @Override
to make another void function inside pickImg()
but I get Annotations are not allowed here
error.
But when I put (based on some answers that I read)
Image IPick = new Image();
Bitmap newI = IPick.pickImg();
public void pickImg()
changes to public Bitmap pickImg()
I get no error but I dont know what or how to return the image to Main since that function has try{}catch{}
for camera intent only and that function is the first function that is needed and the one that will be called for getting image.
Any help/suggestion/getaround?
For those who will ask why I created 2 class instead of using 1, I created different class to avoid long codes in Main.java since I still have things to put on my app.
[EDIT-TL;DR] I can take image with camera when i put my codes on Main.java. But if I put my codes on Image.java and call the function on Main.java, that is where im having a problem. And I can't seem to put the cropped image to imageview(app crashes).