简体   繁体   中英

Kotlin: Code inside lamda function is unreachable while using library written in java

I have used an image picker library (written in Java). It provides the pick-result to the caller activity using the overridden function. As my caller activity is in kotlin, the overridden function gets converted into lambda function. But the code inside lambda function doesn't get executed. Is this a Java-to-Kotlin issue? Or is there a mistake in code?

Here is my code:

   UtilLib.getPhoto(this, ChooseType.REQUEST_ANY)
                .enqueue({ path ->
                    Picasso.with(applicationContext)
                            .load("file://$path")
                            .error(R.drawable.profile)
                            .resize(150, 150)
                            .into(ivProfilePic)
                })

Library link: https://github.com/VinayRathod/UtilLib

Here is the Java Code also which will work perfectly(Java).

            ChooseType chooseType =
                    ((RadioButton) findViewById(R.id.choose_gallery)).isChecked() ? ChooseType.REQUEST_PICK_PICTURE :
                            ((RadioButton) findViewById(R.id.choose_camera)).isChecked() ? ChooseType.REQUEST_CAPTURE_PICTURE :
                                    ChooseType.REQUEST_ANY;
            UtilLib.getPhoto(this, chooseType)
                    .enqueue(new OnImageChooserListener() {

                        @Override
                        public void onImageChoose(String path) {
                            ivPath.setText("" + path);
                            Glide.with(MainActivity.this).load(new File(path)).into(iv);
                        }
                    });

I tried your code like this :

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.util.Log
import com.vinay.utillib.UtilLib
import com.vinay.utillib.imagechooser.ChooseType

import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        setSupportActionBar(toolbar)

        fab.setOnClickListener { view ->
            UtilLib.getPhoto(this, ChooseType.REQUEST_ANY)
                    .enqueue({ path ->
                        Log.d("ok", "executed: $path")
                    })
        }
    }
}

And it seems to work : D/ok: executed: /storage/emulated/0/bichooser/1530711668746.myimage.png

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM