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