GridView不会显示来自Web服务器的图像

时间:2016-04-28 15:14:18

标签: android

我创建了一个应用程序,它将获取图片并将其显示在GridView中。 图像是基于数据库的base64编码,在我的应用程序中我解码图像。 我使用jtds.jar库文件来连接昏迷数据库 当服务器是本地的时,应用程序工作正常 当在Web服务器上时,图像不会出现在Gridview上。 坦克的帮助。

public class HomeFragment扩展了Fragment {

ConnectionClass connectionClass;

GridView gridView;

// ProgressBar pbbar;

MenuProdutoAdapter adapter;

public HomeFragment() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    connectionClass = new ConnectionClass();



        FillGrid fillGrid = new FillGrid();
        fillGrid.execute("");

        Toast.makeText(getActivity(), "Internet nao esta disponivel", Toast.LENGTH_LONG).show();



}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_home, container, false);

    gridView = (GridView) rootView.findViewById(R.id.gridView1);

 //   pbbar = (ProgressBar) rootView.findViewById(R.id.pbbar);
 //   pbbar.setVisibility(View.GONE);

    // Inflate the layout for this fragment


    return rootView;
}


@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
}

@Override
public void onDetach() {
    super.onDetach();
}


public class FillGrid extends AsyncTask<String, String, String> {
    String z = "";

    List<MenuProdutos> menuProdutosList = new ArrayList<MenuProdutos>();


    @Override
    protected void onPreExecute() {

     //   pbbar.setVisibility(View.VISIBLE);
    }

    @Override
    protected void onPostExecute(String r) {

      //  pbbar.setVisibility(View.GONE);

        Toast.makeText(getActivity(),r, Toast.LENGTH_SHORT).show();

        adapter = new MenuProdutoAdapter(HomeFragment.this, menuProdutosList);

        gridView.setAdapter(adapter);

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String pid = menuProdutosList.get(position).getProdutoMenu_ID();

                Toast.makeText(getActivity().getApplicationContext(), "Produtos "
                                + (position + 1) + ": " + menuProdutosList.get(position).getProdutoMenu_nome()
                                + "E o seu PID eh: " + pid,
                        Toast.LENGTH_LONG).show();

// Intent intent = new Intent(getActivity()。getApplicationContext(),FriendsActivity.class); // intent.putExtra(“pid”,pid); // startActivity(intent);

                displayView(position);

                }
        }   );


    }

    @Override
    protected String doInBackground(String... params) {

        try {
            Connection con = connectionClass.CONN();
            if (con == null) {
                z = "Error in connection with SQL server";
            } else {
                String query = "select * from ImagemMenu";
                PreparedStatement ps = con.prepareStatement(query);
                ResultSet rs = ps.executeQuery();


                while (rs.next()) {

                    String Menupro_ID = rs.getString("Id");
                    String Menupro_nome = rs.getString("Nome");
                    String Menupro_image = rs.getString("Imagem");


                    byte[] decodeString = Base64.decode(Menupro_image, Base64.DEFAULT);
                    Bitmap Menupro_bitmap = BitmapFactory.decodeByteArray(decodeString,
                            0, decodeString.length);

                    MenuProdutos p = new MenuProdutos(Menupro_ID, Menupro_nome, Menupro_bitmap);

                    menuProdutosList.add(p);

                }



                z = "Success";
            }
        } catch (Exception ex) {
            z = "Error retrieving data from table";

        }
        return z;
    }
}

1 个答案:

答案 0 :(得分:0)

试试这个

1)从格拉德广场进口毕加索

`compile&#39; com.squareup.picasso:picasso:2.5.2&#39;

2)获取你的imageView并从这里的网址加载图片

 Picasso.with(context).load("http://your_uri").into(imageView);

我希望这个帮助

http://square.github.io/picasso/&lt; - 此处的其他说明

如果你不喜欢Picasso,你可以使用&#34; Universal Image Loader&#34; https://github.com/nostra13/Android-Universal-Image-Loader或Glide https://github.com/bumptech/glide