Android 字节到位图
我在 iPhone 应用程序中运行得很好,但在 Android 中遇到了问题。我在两个应用程序中使用相同的网址/数据。当我将 ListView 中的图像设置为来自字节的位图时,该图像不会出现。数据就在那里。这是我分配视图的代码:
if (camera.snapshot != null)
{
bMap = BitmapFactory.decodeByteArray(camera.snapshot, 0, camera.snapshot.length);
image.setImageBitmap(bMap);
}
这是我将字符串数据转换为字节的地方:
camera.snapshot = responseData.getBytes();
图像是 PNG 文件。它们的大小大约是列表视图图像所需大小的 4 倍,但我认为它们的大小会完美地符合我设置的 ImageView 的边界。
在 iPhone 上,我只是使用 NSData,然后使用 ImageView 中的预构建方法将其转换为图像。它工作完美!我在这里缺少什么?
I have this working just fine in our iPhone app, but am having problems in Android. I'm using the same urls/data in both apps. When I set my image in my ListView to the bitmap that came from the bytes, the image doesn't appear. The data is there. Here is the code where I assign the view:
if (camera.snapshot != null)
{
bMap = BitmapFactory.decodeByteArray(camera.snapshot, 0, camera.snapshot.length);
image.setImageBitmap(bMap);
}
This is where I convert the string data into bytes:
camera.snapshot = responseData.getBytes();
The images are PNG files. They come in about 4 times the size that I need them for the listview image but I would think they would size perfectly to the bounds I set the ImageView to be.
On iPhone I simply use NSData and then use a prebuilt method in ImageView to turn it into an image. It works perfectly! What am I missing here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能需要使用
decodeByteArray
的 4 参数版本:请参阅 http://developer.android.com/reference/android/graphics/BitmapFactory.html#decodeByteArray%28byte[],%20int, %20int,%20android.graphics.BitmapFactory.Options%29。这些选项取决于 PNG 图像的类型,因此您可能需要进行试验。对于通用的 PNG 来说,也许是这样的?
您可以看到 http://developer.android.com/reference/android/ Graphics/BitmapFactory.Options.html 和 http://developer.android.com/reference/android/graphics/Bitmap.Config.html 了解更多详细信息。
You probably need to use the 4-argument version of
decodeByteArray
: see http://developer.android.com/reference/android/graphics/BitmapFactory.html#decodeByteArray%28byte[],%20int,%20int,%20android.graphics.BitmapFactory.Options%29.The options would depend on the type of PNG image, so that you might need to experiment with. For a generic PNG, maybe something like this?
You can see http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html and http://developer.android.com/reference/android/graphics/Bitmap.Config.html for more detail.
这里一切都很好。因此,您需要进行调试以尝试找出问题所在。即 Camera.snapshot = null ?即您可能无法正确获取数据。或者显示图像视图的布局也可能存在问题。尝试将预定义图像设置为 imageview 并查看是否显示。这样您就能够跟踪问题。
Everything is fine here. So you need to debug to try and find where else is the issue. i.e. is Camera.snapshot = null ? i.e. you might not be getting the data properly. Or there could also be an issue in the layouts to show the imageview. Try setting a predefined image to imageview and see if it is shown. This way you would be able to track the problem.