以下应用程序按预期工作:
import android.app.Activity
import android.content.Intent
import android.graphics.BitmapFactory
import android.app.WallpaperManager
class ChwallActivity < Activity
def onCreate(state)
super
setContentView R.layout.main
end
$Override
def onStart
super
Intent intent = Intent.new(Intent.ACTION_PICK)
intent.setType "image/*"
startActivityForResult Intent.createChooser(intent, "Select Picture"), 0
end
$Override
def onActivityResult(requestCode, resultCode, data:Intent)
super
thumb = BitmapFactory.decodeFile "/storage/sdcard0/download/foo.jpg"
manager = WallpaperManager.getInstance self
manager.setBitmap thumb
end
end
这会在无限循环中执行一个gallery-picker,这是不可取的。但是,如果我在finish
函数的末尾插入onStart()
,则似乎不会调用onActivityResult()
:壁纸不会更改为foo.jpg。画廊第二次启动时是否会调用onActivityResult()
?发生了什么事?
答案 0 :(得分:2)
将以下代码移至onCreate
,它应该可以正常运行
Intent intent = Intent.new(Intent.ACTION_PICK)
intent.setType "image/*"
startActivityForResult Intent.createChooser(intent, "Select Picture"), 0
答案 1 :(得分:0)
将finish
放在onActivityResult()
中。
答案 2 :(得分:0)
OnStart()
,再次启动选择器“活动”,依此类推。
您不应该将该逻辑放在onStart()
中,而是让用户事件启动选择器。
OR
您可以使用boolean
标记来跟踪您是否已经选择了图片。