0

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).

Ray
  • 50
  • 11
  • Possible duplicate of [Android ACTION\_IMAGE\_CAPTURE Intent](https://stackoverflow.com/questions/1910608/android-action-image-capture-intent) – A.Edwar Apr 03 '18 at 14:21
  • can't find any solutions there that can help me fix the calling the camera intent from Image class. and returning it to Main. – Ray Apr 03 '18 at 14:48
  • correct me if I'm wrong, but you are trying to call the camera app and return with a result, right? – A.Edwar Apr 03 '18 at 14:52
  • yes. Main should get image from Image. after that Main will put it in ImageView. i have no problem on my Image class except on Main calling the function. is there a solution in that link that i didnt get as solution? – Ray Apr 03 '18 at 14:56
  • The Image Class you created will never return the image from the camera, it will be returned to the activity in it's "onActivityResult" so your code for viewing the image should be moved to "onActivityResult" after retrieving the image – A.Edwar Apr 03 '18 at 15:02
  • i can put imageview1setImageBitmap(cropImage); on "onActivityResult"(Image.java), but how should i call pickImage() without this errors? – Ray Apr 03 '18 at 15:07
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/168133/discussion-between-a-edwar-and-ray). – A.Edwar Apr 03 '18 at 15:09

1 Answers1

0

Create the constructor of Image class and pass the MainActivity reference in constructor.
for e.g.

class Image
{
 private MainActivity mActivity;

 public Image(MainActivity activity){
        this.mActivity=activity;
       }
}

& then call your main activity methods from image class.

mActivity.yourActivityMethod();
Däñish Shärmà
  • 2,891
  • 2
  • 25
  • 43
  • where should i put this codes? i mean what part exactly? I have Cannot resolve symbol mActivity. – Ray Apr 03 '18 at 14:41