开发者

Android widget vanishes from emulator on remoteviews update

开发者 https://www.devze.com 2023-01-16 02:03 出处:网络
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

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开发者_Python百科[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>


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


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号