ImageButton没有'得到'位图

时间:2015-04-22 17:22:27

标签: android

我有以下功能下载图片

private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {

    private ImageButton bmImage;

    public DownloadImageTask(ImageButton bmImage) {
        this.bmImage = bmImage;
    }

    protected void onPreExecute() {

        Log.wtf("IMAGETASK","onPREexecute");
        //mDialog = ProgressDialog.show(ChartActivity.this,"Please wait...", "Retrieving data ...", true);
    }

    protected Bitmap  doInBackground(String... urls) {

        Bitmap bitmap = null;
        String error_message = null;

        for(String url:urls){
            HttpUriRequest request = new HttpGet(url.toString());
            HttpClient httpClient = new DefaultHttpClient();
            try {
                HttpResponse response = httpClient.execute(request);
                StatusLine statusLine = response.getStatusLine();
                int statusCode = statusLine.getStatusCode();
                if (statusCode == 200) {
                    HttpEntity entity = response.getEntity();
                    byte[] bytes = EntityUtils.toByteArray(entity);
                    Log.e("here",""+bytes.length);
                    bitmap = BitmapFactory.decodeByteArray(bytes, 0,bytes.length);
                    Thread.sleep(1000);
                }
                else
                {
                    error_message="Download failed, HTTP response code "+ statusCode + " - " + statusLine.getReasonPhrase();
                }
            } catch (Exception er) {
                Log.e("Error",""+er);
            }}
        //image.setImageBitmap(bitmap);
        return bitmap ;
    }

    /*  OLD ONE (DOESN'T ALSO WORK)
    protected Bitmap doInBackground(String... urls) {
        String urldisplay = urls[0];
        Bitmap mIcon11 = null;
        try {
            InputStream in = new java.net.URL(urldisplay).openStream();
            mIcon11 = BitmapFactory.decodeStream(in);
        } catch (Exception e) {
            Log.wtf("Error", "image download error");
            Log.wtf("Error", e.getMessage());
            e.printStackTrace();
        }
        return mIcon11;
    }*/

    protected void onPostExecute(Bitmap result) {
        Log.wtf("IMAGETASK","onPOSTexecute");
        Log.wtf("IMAGETASK, POST EXECUTE",result.toString());
        //set image of your imageview
        bmImage.setImageBitmap(result);

        android.view.ViewGroup.LayoutParams params = bmImage.getLayoutParams();
        params.height = 50;
        params.width = 50;
        bmImage.setLayoutParams(params);

        int width = bmImage.getWidth();
        int height = bmImage.getHeight();

        Log.wtf("width",String.valueOf(width));
        Log.wtf("height",String.valueOf(height));
        layout.addView(bmImage); //APPENDING ELEMENT (WITH IMAGE)

    }
}

以及以下代码创建元素

private void buildElement(final String friend_id, final String user_id, String name){

    ImageButton element = (ImageButton) getLayoutInflater().inflate(R.layout.ELEMENT_LAYOUT, something, false);

    String img_value = "VALID IMAGE LINK"

        element.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                focus();

            }
        });

        new DownloadImageTask(element).execute(img_value);

}

这是 ELEMENT_LAYOUT.xml

<ImageButton
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    style="?android:attr/buttonStyleSmall"
    android:text="New Button"
    android:layout_width="30dp"
    android:layout_height="30dp"
    android:scaleType="fitXY"
    android:layout_alignParentTop="true"/>

ImageButton的创建确实但是它只是一个很小的灰色方块。有什么想法吗?此外,Log.wtf("width",String.valueOf(width)); Log.wtf("height",String.valueOf(height));记录了&#39; 0&#39;,&#39; 0&#39;。非常感谢你......

0 个答案:

没有答案