如何从 png 文件加载 ImageView?
我使用相机拍照。
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE );
startActivityForResult( intent, 22 );
当活动完成时,我将位图图片写入 PNG 文件。
java.io.FileOutputStream out = openFileOutput("myfile.png", Context.MODE_PRIVATE);
bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
一切顺利,我可以看到该文件是在我的应用程序私有数据空间中创建的。
当我稍后想使用 ImageView 显示该图像时遇到困难。
任何人都可以建议代码来执行此操作吗?
如果我尝试创建一个包含路径分隔符的文件,则会失败。 如果我尝试从不带分隔符的名称创建 Uri,则会失败。
我可以使用以下方式打开文件:
java.io.FileInputStream in = openFileInput("myfile.png");
但这并没有给我设置图像所需的Uri 摘要
iv.setImageURI(u)
:我在私有应用程序数据中的 png 文件中有图片。将其设置到 ImageView 中的代码是什么?
谢谢。
I take a picture with the camera using
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE );
startActivityForResult( intent, 22 );
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.
I'm having difficulty when I later want to display that image using an ImageView.
Can anyone suggest code to do this?
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.
I can open the file OK using:
java.io.FileInputStream in = openFileInput("myfile.png");
But that doesn't give me the Uri I need to set an image with
iv.setImageURI(u)
Summary: I have the picture in a png file in private app data. What's the code to set that into an ImageView?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
尝试
BitmapFactory.decodeFile()
,然后在ImageView
上尝试setImageBitmap()
。Try
BitmapFactory.decodeFile()
and thensetImageBitmap()
on theImageView
.也可能:
Also possible:
为什么不这样:
Why not this way:
decodeStream()
和decodeFile()
之间应该没有区别。decodeFile()
方法打开输入流并调用decodeStream()
。这个问题已经在此链接中得到解答There should be no difference between
decodeStream()
anddecodeFile()
.decodeFile()
method opens an inputstream and callsdecodeStream()
. This was already answered at this link