图像不';无法在android图像视图中显示
我编写了一个Restful web服务。我在mySql中像[BLOB-10b]一样存储了图像数据。我没有任何错误。但是每当我设置图像时,图像都不会显示在Imageview中 我的客户端程序是:图像不';无法在android图像视图中显示,android,android-layout,android-intent,android-fragments,Android,Android Layout,Android Intent,Android Fragments,我编写了一个Restful web服务。我在mySql中像[BLOB-10b]一样存储了图像数据。我没有任何错误。但是每当我设置图像时,图像都不会显示在Imageview中 我的客户端程序是: public class AdminActivity extends Activity { private static final String SERVICE_URL = IPADDRESS.ipAddress + "/OrderFreeWS/HotelReg"; ImageVie
public class AdminActivity extends Activity {
private static final String SERVICE_URL = IPADDRESS.ipAddress
+ "/OrderFreeWS/HotelReg";
ImageView imgView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.adminpage);
imgView = (ImageView) findViewById(R.id.imgViewRetrive);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
}
public void retriveImage(View v) {
String sampleURL = SERVICE_URL;
WebServiceTask wst = new WebServiceTask(WebServiceTask.GET_TASK, this,
"GETting Image...");
wst.execute(new String[] { sampleURL });
}
private class WebServiceTask extends AsyncTask<String, Integer, String> {
public static final int POST_TASK = 1;
public static final int GET_TASK = 2;
private static final String TAG = "WebServiceTask";
// connection timeout, in milliseconds (waiting to connect)
private static final int CONN_TIMEOUT = 3000;
// socket timeout, in milliseconds (waiting for data)
private static final int SOCKET_TIMEOUT = 5000;
private int taskType = POST_TASK;
private Context mContext = null;
private String processMessage = "Processing...";
private ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
private ProgressDialog pDlg = null;
public WebServiceTask(int taskType, Context mContext,
String processMessage) {
this.taskType = taskType;
this.mContext = mContext;
this.processMessage = processMessage;
}
public void addNameValuePair(String name, String value) {
params.add(new BasicNameValuePair(name, value));
}
private void showProgressDialog() {
pDlg = new ProgressDialog(mContext);
pDlg.setMessage(processMessage);
pDlg.setProgressDrawable(mContext.getWallpaper());
pDlg.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pDlg.setCancelable(false);
pDlg.show();
}
@Override
protected void onPreExecute() {
/*hideKeyboard();*/
showProgressDialog();
}
protected String doInBackground(String... urls) {
String url = urls[0];
String result = "";
Log.d("haiiii", "Haiiiiiiiiiiiiiiii");
HttpResponse response = doResponse(url);
if (response == null) {
Log.d("haiiii", "Haiiiiiiiiiiiiiiii nooooo");
return result;
} else {
try {
Log.d("haiiii", "Haiiiiiiiiiiiiiiii yesssss");
result = inputStreamToString(response.getEntity()
.getContent());
} catch (IllegalStateException e) {
Log.e(TAG, e.getLocalizedMessage(), e);
} catch (IOException e) {
Log.e(TAG, e.getLocalizedMessage(), e);
}
}
return result;
}
@Override
protected void onPostExecute(String response) {
Log.d("Success", "successfully comes here...,");
handleResponse(response);
pDlg.dismiss();
}
private void handleResponse(String response) {
try {
System.out.println("enteringggggg1111");
JSONObject jso=new JSONObject(response);
byte[] imgLogo=Base64.decode(jso.getString("hotel_logo"),Base64.DEFAULT);
Log.d("enteringggggg2222","enteringggggg2222");
Bitmap b1=BitmapFactory.decodeByteArray(imgLogo,0,imgLogo.length);
Log.d("enteringggggg33333","enteringggggg333333");
imgView.setImageBitmap(b1);
Toast.makeText(getBaseContext(),"UPLOADED IMAGE SUCCESSFULLY",Toast.LENGTH_SHORT).show();
} catch (JSONException e) {
e.printStackTrace();
}
}
// Establish connection and socket (data retrieval) timeouts
private HttpParams getHttpParams() {
HttpParams htpp = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(htpp, CONN_TIMEOUT);
HttpConnectionParams.setSoTimeout(htpp, SOCKET_TIMEOUT);
return htpp;
}
private HttpResponse doResponse(String url) {
// Use our connection and data timeouts as parameters for our
// DefaultHttpClient
HttpClient httpclient = new DefaultHttpClient(getHttpParams());
HttpResponse response = null;
try {
switch (taskType) {
case POST_TASK:
HttpPost httppost = new HttpPost(url);
// Add parameters
httppost.setEntity(new UrlEncodedFormEntity(params));
response = httpclient.execute(httppost);
break;
case GET_TASK:
HttpGet httpget = new HttpGet(url);
response = httpclient.execute(httpget);
break;
}
} catch (Exception e) {
Log.e(TAG, e.getLocalizedMessage(), e);
}
return response;
}
private String inputStreamToString(InputStream is) {
String line = "";
StringBuilder total = new StringBuilder();
// Wrap a BufferedReader around the InputStream
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
try {
// Read response until the end
while ((line = rd.readLine()) != null) {
total.append(line);
}
} catch (IOException e) {
Log.e(TAG, e.getLocalizedMessage(), e);
}
// Return full string
return total.toString();
}
}
我已从数据库中检索到值。
但当我设置为图像视图时,它不会显示。从数据库中检索图像时的值是多少?#Achiles我从数据库中获取blob值。类似于[blob-10 B]转换为字节数组,然后位图并将其设置为imageview。发布stacktrace。如果出现任何错误,请不要发布整个类。只发布代码的相关部分。#Achiles我获取字节数组值并将其设置为imageview,如
JSONObject jso=new JSONObject(response);字节[]imgLogo=Base64.decode(jso.getString(“hotel_logo”),Base64.DEFAULT);位图b1=位图工厂.decodeByteArray(imgLogo,0,imgLogo.length);imgView.setImageBitmap(b1)代码>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/imgViewRetrive"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/textView1"
android:layout_marginTop="38dp"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:text="Admin page"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/imgViewRetrive"
android:layout_marginTop="18dp"
android:onClick="retriveImage"
android:text="save" />
@Path("/HotelReg")
public class HotelRegsiter {
private HotelData hotel=new HotelData();
ResultSet rs=null;
Statement stmt=DbConnection.StartConnection();
@Context
Request request;
@Context
UriInfo uriInfo;
@GET
@Produces(MediaType.APPLICATION_JSON)
public HotelData getHotel() throws SQLException {
System.out.println("WELCOME TO ORDER FREE GET");
rs = stmt.executeQuery("select hotel_logo from hotel where id=16;");
while (rs.next()) {
hotel.setHotel_logo(rs.getBytes("hotel_logo"));
}
return hotel;
}
}