【问题标题】:Android widget vanishes from emulator on remoteviews updateAndroid 小部件在远程视图更新时从模拟器中消失
【发布时间】: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();
?>
  1. httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    InputStream is = entity.getContent(); 
    response = httpclient.execute(httppost);
    HttpEntity entity = response.getEntity();
    is = entity.getContent();
  2. 将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();
  3. 将字节数组转换为位图,并将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


【解决方案1】:

遇到了问题 - 位图返回 Null,因此小部件初始布局消失了。问题出在 php 中。

【讨论】:

    【解决方案2】:

    现在可以了!!!在浏览器和应用上

    新的php:

    <?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();?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-02
      • 1970-01-01
      • 1970-01-01
      • 2016-11-04
      • 2014-01-16
      • 1970-01-01
      • 2012-08-18
      相关资源
      最近更新 更多