使用Kotlin Anko拍摄照片并显示结果的最佳方法是什么?

我正在和Anko一起玩Kotlin,并试图用它拍摄一张照片,然后在我的测试应用程序中使用下面的Android培训文档作为起点显示结果图像。

http://developer.android.com/training/camera/photobasics.html

使用Anko实现这个功能的最好方法是什么?

我试图使用startActivityForResult函数,但我没有任何运气搞清楚如何将图像视图添加到可以使用onActivityResult的结果更新的布局。

我将粘贴一些我的代码,我使用毕加索从Url呈现图像到一个图像视图这是一个很好的做法,因为它会为您处理最佳的一切:

import kotlinx.android.synthetic.main.activity_login.* class LoginActivity : AppCompatActivity() { val imageReturned = 100 override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { super.onActivityResult(requestCode, resultCode, data) //With when we select the request code we asked in the StartActivityForResult when(requestCode){ //that will be imageReturned imageReturned -> Picasso.with(this@LoginActivity) .load(data?.getStringExtra("theUrl")).into(image_view) //now what we did here was use Picasso to load //the URL in the StringExtra "theUrl" into image_view, //image_view will be the synthetic bound of anko for it to //work you have to `import kotlinx.android.synthetic.main.activity_login.*` } } } 

这是如果你没有绑定你的视图,如果你没有,并使用anko DSL来实现它,那么你必须得到contentView与findViewById(android.R.id.content),然后搜索您的ImageView的id然后设置它和我在那里一样。

希望它有帮助

如果你之前在Java中使用过Android开发,那么除了语法之外,它与Java没有什么不同。

教程就够了。 尝试遵循这一点,并使用Android Studio Kotlin插件将Java文件转换为Kotlin。 您将自己学习和弄清楚如何将Java代码映射到Kotlin,反之亦然。

  1. 如果你还没有安装Android Studio的Kotlin插件
  2. 打开Java文件
  3. 单击Help > Find Action菜单以打开查找操作
  4. 搜索“将Java文件转换为Kotlin文件”

关于你在onActivityResult设置图片的问题(关于你提到的教程)。 您可以使用dispatchTakePictureIntent()定义的UriImageView进行设置。