2010-09-12 26 views
1

我的MySQL数据库表包含一个BLOB图像,其中image_id作为关键字。我正在尝试做以下事情。Android widget从remoteviews上的模拟器中消失更新

1.来自Android应用程序的HttpPost(“http://example.com/RetrieveImage.php”)与名称值对中的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();
  • 存储BLOB数据中的InputStream目的是字节数组

    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设置为图像

    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>

    +0

    尝试。它被弃用,它需要孩子的绝对位置。尝试LinearLayout。 – bhups 2010-09-13 03:48:41

    +0

    尝试线性布局仍然无法正常工作....但我发现问题仍然需要一些解决此问题的帮助。 – Aakash 2010-09-13 05:35:17

    +0

    来自MySQL DB的BLOB被检索到并且被字节数组字节接收。这是最有可能不起作用的东西bitmap = BitmapFactory.decodeByteArray(bytes,0,bytes.length);我认为这会将字节数组转换成一个保存为BLOB的位图图像,但总是返回null,这就是为什么这个小部件是GONE。请帮忙。 – Aakash 2010-09-13 05:36:46

    回答

    1

    出现问题 - 位图返回Null,所以小部件初始布局消失。问题在于php。

    0

    现在它工作!在浏览器和应用程序

    新的PHP:使用一些其他的布局而不是AbsoluteLayout

    <?php 
    mysql_connect("host","login","password"); 
    mysql_select_db("dbname"); 
    $q=mysql_query("SELECT picture FROM tablename WHERE id>='".$_REQUEST['id']."'"); 
    
    $e=mysql_fetch_row($q); 
    header('Content-Length: '.strlen($e[0])); 
    header('Content-type: image/jpg'); 
    echo $e[0]; 
    mysql_close();?>