如何在Bitmap中从ImageView显示图像... ImageView变量返回null

时间:2013-11-07 14:56:04

标签: android imageview bitmapfactory

我在从位图显示图像时遇到问题。 我从JsonObject获取base64编码的字符串并解码后获取字节数组和位图。 我很不走运,所以我现在尝试使用drawable中的相同图像。

现在问题是我在imageView中设置图像位图时得到空指针异常 并且不知道为什么......

请阅读:ImageView variable returning null even after defined; Android

全班的代码是:`package com.statsports.subjectiveandroid;

public class PlayersListActivity extends Activity {

    // URL to make request
    private static String URL = "http://000.000.00.0/index.php";
    private static int userID;
    ArrayList<HashMap<String, Object>> playersList;
    ListView playerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.list_view);

        Bundle extras = getIntent().getExtras();
        userID = extras.getInt("id");
        initLayout();
    }

    private void initLayout() {

        playerView = (ListView) findViewById(R.id.list);
        playersList = new ArrayList<HashMap<String, Object>>();

        playerView.setAdapter(new SimpleAdapter(PlayersListActivity.this,
                playersList, R.layout.activity_players_list, null, null));

        //playerView.setDividerHeight(0);

    }

    @Override
    protected void onResume() {
        if (!ConnectivityStatus.isNetworkAvailable(getApplicationContext())) {
            Toast.makeText(PlayersListActivity.this, R.string.network_failed,
                    Toast.LENGTH_SHORT).show();
        } else {
            playerView.setAdapter(null);
            new PlayersLoadTask().execute();
        }

        super.onResume();
    }

    ProgressDialog pDialog;

    class PlayersLoadTask extends AsyncTask<String, String, String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(PlayersListActivity.this);
            pDialog.setMessage("Reading data");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
            playersList.clear();
        }

        @Override
        protected String doInBackground(String... arg0) {
            try {

                ArrayList<BasicNameValuePair> parameters = new ArrayList<BasicNameValuePair>();
                parameters.add(new BasicNameValuePair("request", "getPlayers"));
                parameters.add(new BasicNameValuePair("clubid", Integer
                        .toString(userID)));

                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(PlayersListActivity.URL);
                httpPost.setEntity(new UrlEncodedFormEntity(parameters,
                        ("ISO-8859-1")));
                HttpResponse httpResponse = httpClient.execute(httpPost);
                HttpEntity httpEntity = httpResponse.getEntity();

                // if this is null the web service returned an empty page
                if (httpEntity == null) // response is empty so exit out
                    return null;

                String jsonString = EntityUtils.toString(httpEntity);

                // again some simple validation around the returned string
                if (jsonString != null && jsonString.length() != 0) // checking
                                                                    // string
                                                                    // returned
                                                                    // from
                                                                    // service
                                                                    // to grab
                                                                    // id
                {
                    JSONArray jsonArray = new JSONArray(jsonString);
                    for (int i = 0; i < jsonArray.length(); i++) {
                        HashMap<String, Object> map = new HashMap<String, Object>();
                        JSONObject jsonObject = (JSONObject) jsonArray.get(i);
                        int id = jsonObject.getInt("id");
                        String name = jsonObject.getString("name");
                        String base64 = jsonObject.getString("image");
                        map.put("id", id);
                        map.put("name", name);
                        if (name == null || name.trim().length() == 0) {
                            name = "Player unknown";
                        } else {
                            map.put("image", (R.drawable.baboon));
                        }

                        Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.walcott);
                        ByteArrayOutputStream stream = new ByteArrayOutputStream();
                        bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
                        byte[] byteArray = stream.toByteArray();
                        Bitmap bmp1 = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
                        ImageView image = (ImageView) findViewById(R.id.image);

                        image.setImageBitmap(bmp1);
                        //ByteArrayOutputStream stream = new ByteArrayOutputStream();
                        //byte[] decodedString = Base64.decode(
                        //      base64.getBytes("UTF-8"), Base64.NO_PADDING);
                        /*
                         * InputStream is = new
                         * ByteArrayInputStream(decodedString); Bitmap bitmap =
                         * BitmapFactory.decodeStream(is);
                         * bitmap.compress(Bitmap.CompressFormat.PNG, 100,
                         * stream);
                         * 
                         * if(bitmap != null){ //Drawable drawableImg = new
                         * BitmapDrawable(getResources(), bitmap); ImageView
                         * imgView = (ImageView) findViewById(R.id.image);
                         * imgView.setImageBitmap(bitmap); map.put("image",
                         * bitmap); }
                         */

                        //String images = new String(decodedString);
                        //Bitmap decodedByte = BitmapFactory.decodeByteArray(
                        //      decodedString, 0, decodedString.length);
                        //decodedByte.compress(Bitmap.CompressFormat.PNG, 100,
                        //      stream);
                        //Log.d("DECODEDBYTE IS: ", decodedByte.toString());

                        //LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                        //View view = inflater.inflate(R.layout.activity_players_list, null);
                        //setContentView(view);
                        //ImageView imgView = (ImageView) findViewById(R.id.image);
                        //imgView.setImageBitmap(decodedByte);

                        playersList.add(map);
                    }
                }
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            } catch (Exception e) {
                Log.e("ERROR SOMEWHERE!!!! ", e.toString());
            }
            return null;

        }

        protected void onPostExecute(String file_url) {
            if (playersList.size() == 0) {
                Toast.makeText(PlayersListActivity.this,
                        "No players in a list", Toast.LENGTH_SHORT).show();
            } else {
                playerView.setAdapter(new PlayerListAdapter(
                        PlayersListActivity.this, playersList,
                        R.layout.activity_players_list, new String[] { "id",
                                "image", "name" }, new int[] {
                                R.id.player_list_id, R.id.image, R.id.name, }));
            }

            pDialog.dismiss();
        }
    }
} 

0 个答案:

没有答案