Android 小部件在 Remoteviews 更新时从模拟器中消失

发布于 2024-09-19 01:35:24 字数 1955 浏览 4 评论 0原文

我的 MySQL DB 表包含一个以 image_id 作为键的 BLOB 图像。我正在尝试执行以下操作。

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();
?>
  1. httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    输入流=entity.getContent(); 
    响应 = httpclient.execute(httppost);
    HttpEntity实体=response.getEntity();
    is =entity.getContent();
  2. 将Inputstream对象中的BLOB数据存储为字节数组

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
            字节[] buf = 新字节[10240];
            整数 n = 0;
            尝试 {
                while ((n=is.read(buf))>=0)
                {
                baos.write(buf, 0, n);
                }
    is.close();
    byte[] bytes = baos.toByteArray();
  3. 将字节数组转换为位图并将Android widget的ImageView设置为Image< /p>

    Bitmap 位图;
    远程视图 updateViews = null;
    bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
    updateViews = new RemoteViews(context.getPackageName(), R.layout.tuwidget);
    updateViews.setImageViewBitmap(R.id.tuwidget_img_btn, 位图);
    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>

My MySQL DB table holds a BLOB image with image_id as key. I am trying to do the following.

1.HttpPost("http://example.com/RetrieveImage.php") from Android App with the image_id in name value pairs.

The PHP Script is as follows:

<?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. Store the BLOB data in Inputstream object is to byte array

    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. Convert the byte array to bitmap and set ImageView of the Android widget to the 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;

Once this is run, I dont know why the widget does not even show up on the emulator and
cannot even add the widget anymore to the screen. What am I doing wrong? Any help is much appreciated.

Oh and here is my widget layout.

<?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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

Oo萌小芽oO 2024-09-26 01:35:24

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

Got the problem - Bitmap was returning Null so the widget initial layout vanished. Issue was in the php.

丑丑阿 2024-09-26 01:35:24

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

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

Now it works!!! on browser and app

new 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();?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文