如何从 png 文件中加载 ImageView? [英] How to load an ImageView from a png file?

查看:25
本文介绍了如何从 png 文件中加载 ImageView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用相机拍照

Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE );
startActivityForResult( intent, 22 );

活动完成后,我将位图图片写入 PNG 文件.

When the activity completes, I write the bitmap picture out to a PNG file.

    java.io.FileOutputStream out = openFileOutput("myfile.png", Context.MODE_PRIVATE);
    bmp.compress(Bitmap.CompressFormat.PNG, 90, out);

没问题,我可以看到文件是在我的应用私有数据空间中创建的.

That goes OK, and I can see the file is created in my app private data space.

稍后我想使用 ImageView 显示该图像时遇到了困难.

I'm having difficulty when I later want to display that image using an ImageView.

任何人都可以建议代码来执行此操作吗?

Can anyone suggest code to do this?

如果我尝试创建一个包含路径分隔符的文件,它会失败.如果我尝试从不带分隔符的名称创建 Uri,则会失败.

If I try to create a File with path separators in, it fails. If I try to create a Uri from a name without separators, that fails.

可以使用以下命令打开文件OK:

I can open the file OK using:

        java.io.FileInputStream in = openFileInput("myfile.png");

但这并没有给我设置图像所需的 Uri

But that doesn't give me the Uri I need to set an image with

   iv.setImageURI(u)

总结:我在私人应用数据中的 png 文件中有图片.将其设置为 ImageView 的代码是什么?

Summary: I have the picture in a png file in private app data. What's the code to set that into an ImageView?

谢谢.

推荐答案

尝试 BitmapFactory.decodeFile() 然后 setImageBitmap()ImageView.

Try BitmapFactory.decodeFile() and then setImageBitmap() on the ImageView.

这篇关于如何从 png 文件中加载 ImageView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆