【发布时间】:2010-09-12 23:38:59
【问题描述】:
我的 MySQL 数据库表包含一个以 image_id 作为键的 BLOB 图像。我正在尝试执行以下操作。
1.HttpPost("http://example.com/RetrieveImage.php") 来自 Android 应用程序,名称值对中的 image_id。
PHP 脚本如下:
<?php
mysql_connect("host","userid","password");
mysql_select_db("database");
$q=mysql_query("SELECT image FROM testblob WHERE image_id =".$_REQUEST['image_id']."");
$e=mysql_fetch_assoc($q);
print($e);
mysql_close();
?>
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); InputStream is = entity.getContent(); response = httpclient.execute(httppost); HttpEntity entity = response.getEntity(); is = entity.getContent();-
将Inputstream对象中的BLOB数据存储到字节数组中
ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buf = new byte[10240]; int n = 0; try { while ((n=is.read(buf))>=0) { baos.write(buf, 0, n); } is.close(); byte[] bytes = baos.toByteArray(); -
将字节数组转换为位图,并将Android小部件的ImageView设置为Image
Bitmap bitmap; RemoteViews updateViews = null; bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length); updateViews = new RemoteViews(context.getPackageName(), R.layout.tuwidget); updateViews.setImageViewBitmap(R.id.tuwidget_img_btn, bitmap); return updateViews;
一旦运行,我不知道为什么小部件甚至没有出现在模拟器上,并且 甚至无法再将小部件添加到屏幕上。我究竟做错了什么?非常感谢任何帮助。
哦,这是我的小部件布局。
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tuwidget"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<ImageView
android:id="@+id/tuwidget_img_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
</ImageView>
</AbsoluteLayout>
【问题讨论】:
-
尝试使用其他布局而不是 AbsoluteLayout。它已弃用,并且需要其子项的绝对位置。试试线性布局。
-
试过线性布局还是不行....但是我发现问题还是需要一些帮助来解决这个问题。
-
从 MySQL DB 中检索 BLOB 并通过字节数组 bytes 接收。这很可能不起作用 bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);我认为这会将字节数组转换为保存为 BLOB 但始终返回 null 的位图图像,这就是小部件消失的原因。请帮忙。
标签: php mysql android widget blob