0

我正在通过 php 从 mysql 获取图像并使用 asyntask<> $b64image 获取它的值正在我的 android 代码中获取。

<?php

include("db.php");

include("database.php");

//header("Content-Type: image/jpeg");



$skt=$_REQUEST['clientsocket'];

$retrieve = SelectSingleRow("screendb"," clientsocket='$skt'","");

 $val=$retrieve['img'];

 $response["img_val"]=$val;

$b64image["base64_image"] = base64_encode($val);

echo json_encode($b64image);

?>

我的 fetch 类是:它没有显示任何错误但没有显示图像。
在 strbase64_image 获得 $b64image 的值。

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Base64;
import android.util.Log;
import android.view.ContextThemeWrapper;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;


public class fetchimg extends Activity {
    int flag;
    int empid;
    String username;
    String password;
    ImageView imageView;
    EditText edUsername,edPassword;

    private ProgressDialog pDialog;

    JSONParser jParser = new JSONParser();
    JSONArray jarray;

    ArrayList<HashMap<String, String>> productsList;

    private static String url_faculty_login = "http://10.0.2.2/image/image_decode.php";
    private static final String TAG_EMPLOYEE = "employee_id";
    private static final String TAG_PID = "pid";
    private static final String TAG_NAME = "name";



    @Override
    public void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);
        setContentView(R.layout.showimage);
    //  setTitleColor(1);
        imageView=(ImageView) findViewById(R.id.imgView);

        Intent i=new Intent();


        edUsername=(EditText) findViewById(R.id.edUserName);
        edPassword=(EditText) findViewById(R.id.edPassword);
        String clear="clear";
        new LoadAllProducts().execute();




    }



    class LoadAllProducts extends AsyncTask<String, String, String> {
        private static final String Emp_id = "Emp_id";
        Bitmap bitmap;
        String strbase64_image;

        public String doInBackground(String... args) {
             username="port=1892";
        //   password=edPassword.getText().toString();

             List<NameValuePair> params = new ArrayList<NameValuePair>();

            params.add(new BasicNameValuePair("clientsocket", username));
        //  params.add(new BasicNameValuePair("Password", password));



            JSONObject json = jParser.makeHttpRequest(url_faculty_login, "GET", params);
            Log.d("base64image_encoded from php ", json.toString());

            strbase64_image=json.toString();

             bitmap =Base64ToImage(strbase64_image);

        return null;
    }
        public Bitmap Base64ToImage(String base64String) {
            byte[] imageAsBytes = Base64.decode(base64String.getBytes(),
                    Base64.DEFAULT);
            Bitmap mybitmap = BitmapFactory.decodeByteArray(imageAsBytes, 0,
                    imageAsBytes.length);
            return mybitmap;
        }
        @Override
        protected void onPostExecute(String result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    imageView.setImageBitmap(bitmap);


                }
            });

        }

}



}
4

2 回答 2

0

AsyncTask的一切都搞砸了,因此为什么它不起作用。

doInBackground没有返回任何东西,所以你onPostExecute永远不会被调用,并且你的返回类型不正确。

尝试改用这个:

class LoadAllProducts extends AsyncTask<Void, Void, Bitmap> {
        private static final String Emp_id = "Emp_id";
        Bitmap bitmap;
        String strbase64_image;

        protected Bitmap doInBackground(Void... args) {
            username = "port=1892";
            // password=edPassword.getText().toString();

            List<NameValuePair> params = new ArrayList<NameValuePair>();

            params.add(new BasicNameValuePair("clientsocket", username));
            // params.add(new BasicNameValuePair("Password", password));

            JSONObject json = jParser.makeHttpRequest(url_faculty_login, "GET",
                    params);
            Log.d("base64image_encoded from php ", json.toString());

            strbase64_image = json.toString();

            bitmap = Base64ToImage(strbase64_image);

            return bitmap;
        }

        private Bitmap Base64ToImage(String base64String) {
            byte[] imageAsBytes = Base64.decode(base64String.getBytes(),
                    Base64.DEFAULT);
            Bitmap mybitmap = BitmapFactory.decodeByteArray(imageAsBytes, 0,
                    imageAsBytes.length);
            return mybitmap;
        }

        protected void onPostExecute(Bitmap result) {

            // TODO Auto-generated method stub
            imageView.setImageBitmap(bitmap);

        }
    }

在这里,您doInBackground返回检索BitmaponPostExecuteImageView. 不需要使用runOnUiThread,因为 postExecute 已经在 UI 线程上运行。

于 2013-04-17T07:50:56.210 回答
0
strbase64_image=json.toString();

应该 :

strbase64_image=json.getString("base64_image");

如表格中json所示JSONObject

{"base64_image": "AAAAAAA(... some BASE64 content ...)"}

因为它是由json_encodephp 代码中的关联数组创建的

于 2013-04-17T07:51:22.277 回答