图像不显示在android imageview中

时间:2014-04-17 06:09:56

标签: android android-layout android-intent android-fragments

我在mySql中写了一个Restful web service.i存储的图像数据,如[BLOB - 10 B]。我没有任何错误。但是每当我设置图像时,该图像都不会在Imageview上显示。

我的客户端程序是:

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();
    }

}

我的xml文件是:

<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" />

我从db.via下面的类中检索图像:

@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;
}

}

我从db中检索了值。 但是当我进入图像视图时它没有显示。

0 个答案:

没有答案