我正在使用Kotlin前端/ Python后端构建应用程序。我的python脚本在getFilesDir()目录(/data/user/0/com.example.inprogress/files/mygraph.png)下创建一个.png文件。然后,我想使用kotlin来获取.png文件并将其显示在imageview上。
我的目标是将位图值分配给我的imageview id graphId。
我使用下面的代码,但得到空白的imageview而不是要显示的.png文件。请注意,我没有收到任何错误消息。我在哪里做错了?
class GraphRes : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_graphres)
var data = intent.extras
var graph = data!!.get("res").toString()
// graph is where the .png file is stored
// graph: /data/user/0/com.example.inprogress/files/mygraph.png
var graphId = ImageView(this)
graphId.setImageBitmap(BitmapFactory.decodeFile(graph))
}
}
您具有READ_EXTERNAL_STORAGE
权限吗?
如果有,但仍然无法正常工作,请尝试以下方法:
graphId.setImageURI(Uri.fromFile(new File(graph)));