2

I want to use a Kotlin Android library (FotoApparat) in a Java Android project.

In a Kotlin code base the whenAvailble function gets a kotlin callback as a param, which will be called when the async operation is done.

val photoResult = fotoapparat.takePicture()

// Asynchronously saves photo to file
photoResult.saveToFile(someFile)

// Asynchronously converts photo to bitmap and returns the result on the main thread
photoResult
    .toBitmap()
    .whenAvailable { bitmapPhoto ->
            val imageView = (ImageView) findViewById(R.id.result)

            imageView.setImageBitmap(bitmapPhoto.bitmap)
            imageView.setRotation(-bitmapPhoto.rotationDegrees)
    }

whenAvailable code can be found here

The equivalent java implementation would be: (Previously the library was written in java)

fotoApparat.takePicture().
                    toPendingResult().
                    whenAvailable( /* some sort of call back */);

How do I provide the whenAvailable callback from a Java code?

In previous versions of the lib, there was a Java pending result callback class which no longer available.

5
  • 2
    i dont get your question: you already posted java code, so what do you need actually? Commented Apr 16, 2018 at 10:40
  • i'll edit to make it clearer Commented Apr 16, 2018 at 10:41
  • If you use Java 8 or higher, you can use a lambda or a method reference, otherwise you need to provide an instance of your callback (e.g., an anonymous class). By the way your original question before the edit contained a possible answer, as said by @pskink Commented Apr 16, 2018 at 10:45
  • short answer: yes you can use a Kotlin library and access it from a Java class Commented Apr 16, 2018 at 10:46
  • @user2340612 the code before the edit, does not work, it used to work for v1.x - if you can get it to work, i'd love to see how. Commented Apr 16, 2018 at 11:27

2 Answers 2

5

Abreslav said:

Unit is a type (unlike void) and has a value (unlike Void). This enable uniform treatment of Unit when it comes to generic classes. I.e. we don’t need another two types: a function that returns something and a function that returns void. It’s all one type: a function that returns something tat may be Unit.

Introducing void would pose many problems in areas like type inference, compositionality of any sort of functions, etc

So, unlike in Kotlin, in Java you need to return this value explicitly.

Your lambda needs to be:

fotoApparat.takePicture().
                toPendingResult().
                whenAvailable(bitmapPhoto -> {
        ImageView imageView = (ImageView) findViewById(R.id.result);
        return Unit.INSTANCE;
});
Sign up to request clarification or add additional context in comments.

Comments

0

Yes you can use something like this:

fotoApparat.takePicture().
                toPendingResult().
                whenAvailable(bitmapPhoto -> {
        ImageView imageView = (ImageView) findViewById(R.id.result);
        // rest of your code
});

3 Comments

maybe it's something with my project settings, but that code has a syntax error. "missing return statement" on the last line.
Is the error shown in the callback of whenAvailable, or in the enclosing method? From the source code you linked, the callback passed to whenAvailable returns Unit (i.e., void), so the error you see shouldn't be linked to that
@user2340612 the error shown in the callback provided to whenAvailable

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.