简体   繁体   English

来自Android中longblob的位图

[英]Bitmap from longblob in android

I have some images stored as longblob s in a remote MySQL database and I need to retrieve them for my Android application and display as a Bitmap . 我有一些图像以longblob的形式存储在远程MySQL数据库中,我需要为我的Android应用程序检索它们并将其显示为Bitmap I have a PHP script setup to parse SQL queries to/from the Android application. 我有一个PHP脚本设置来解析来自/来自Android应用程序的SQL查询。

What I have tried: I tried to get a String as below of the longblob field and then used getBytes() on it. 我尝试过的事情:我尝试获取longblob字段下面的String,然后在其上使用getBytes() But apparently, it isn't working. 但显然,它不起作用。 If I display the String, a huge number of characters (readable as well as unreadable) are displayed. 如果我显示字符串,则会显示大量字符(可读和不可读)。 So the problem is with encoding or converting from String to byte[] (I think). 所以问题是编码或从String转换为byte[] (我认为)。

StringBuilder strStream = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(httpEntity.getContent()), 8);

String line = null;
while((line = reader.readLine()) != null) strStream.append(line);

Bitmap bitmap = BitmapFactory.decodeByteArray(strStream.toString().getBytes(), 0, strStream.length());

What I Need: Is there any way to create a Bitmap from a remote longblob stored in a MySQL database which I connect from Android using a custom PHP script? 我需要什么:有什么方法可以从存储在MySQL数据库中的远程longblob创建Bitmap ,我使用自定义PHP脚本从Android连接到我的MySQL数据库? I'm perfectly sure the script is working correctly and my code is 100% working up to the variable httpEntity . 我完全确定脚本可以正常工作,并且我的代码可以100%处理变量httpEntity I can even get the String and its the correct value. 我什至可以获取String及其正确的值。 The only problem is, I can't get a Bitmap . 唯一的问题是,我无法获得Bitmap

Thanks. 谢谢。

The simpler, the better :) PHP can output a blob from a database 更简单,更好:) PHP可以从数据库输出blob

header('Content-Type: image/png');
echo $result['blob'];

When you request this script with a web browser, you'll see a PNG image. 当您通过网络浏览器请求此脚本时,您会看到一个PNG图片。 Then the Android part 然后是Android部分

InputStream in = new URL("http://img.io/read.php").openStream();
Bitmap bmp = BitmapFactory.decodeStream(in);
in.close();

There's no need to pass through Strings here... Strings in Java are not simply byte arrays. 这里不需要传递字符串... Java中的字符串不仅仅是字节数组。 They carry semantics, so the byte array is always coupled with an encoding scheme (which defaults to UTF-16LE) 它们具有语义,因此字节数组始终与编码方案(默认为UTF-16LE)结合使用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM