将位图值转换为Imageview

时间:2013-04-17 07:35:13

标签: android bitmap imageview

我通过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);

&GT;

我的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);


                }
            });

        }

}



}

2 个答案:

答案 0 :(得分: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会返回已检索的Bitmap,并将其传递给onPostExecuteImageView会更新您的runOnUiThread。无需使用{{1}},因为postExecute已在UI线程上运行。

答案 1 :(得分:0)

strbase64_image=json.toString();

应该是:

strbase64_image=json.getString("base64_image");

由于json是以{/ p>形式表示的JSONObject

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

json_encode在php代码

中的关联数组上创建