java - ImageURL 未填充在 Android 中 imageView 内的图像中

标签 java android android-fragments bitmap imageview

我在从我使用位图的 url 中填充 imageview 中的图像时遇到问题,但它不起作用 我使用输出语句来检查 logcat 中的 url,url id 显示正常,但是当涉及到位图时,它不会从 mysql 数据库动态填充 url 中的图像

public class StoreHomeFragment extends Fragment {
View rootView;
ImageView imageview;
final ArrayList<HashMap<String, String>> MyArrList = new ArrayList<HashMap<String, String>>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {

rootView = inflater.inflate(R.layout.store_home, container, false); 
return rootView;
}



@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    new AsyncTask<Void, Void, Void>() {

        @Override
        protected Void doInBackground(Void... params) {
            String url = "http://192.168.1.132/Android/App/good.php";

            try {
                JSONArray data = new JSONArray(getJSONUrl(url));

                HashMap<String, String> map;

                for (int i = 0; i < data.length(); i++) {
                    JSONObject c = data.getJSONObject(i);

                    map = new HashMap<String, String>();
                    map.put("name", c.getString("name"));
                    map.put("artist", c.getString("artist"));
                    map.put("price", c.getString("price"));
                    map.put("image", c.getString("image"));

                    MyArrList.add(map);
                }

            } catch (JSONException e) {

                e.printStackTrace();
            } catch (Exception e) {

                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            final GridView gridView1 = (GridView) rootView.findViewById(R.id.store_home_gridview);
            gridView1.setAdapter(new ImageAdapter(getActivity(), MyArrList));

        }
    }.execute();}      

public class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {  //not working
ImageView bmImage;

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

  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) {
          bmImage.setImageResource(android.R.drawable.ic_menu_report_image);
      }
      return mIcon11;
  }

  protected void onPostExecute(Bitmap res) {
        bmImage.setImageBitmap(res);
  }
}

class ImageAdapter extends BaseAdapter {
private Context context;
public ImageView imageView;
private ArrayList<HashMap<String, String>> MyArr = new ArrayList<HashMap<String, String>>();

public ImageAdapter(Context c,ArrayList<HashMap<String, String>> list) 
{   
    context = c;

    MyArr = list;
}

public int getCount() {

    return MyArr.size();
}

public Object getItem(int position) {

    return position;
}

public long getItemId(int position) {

    return position;
}
public View getView(int position, View convertView, ViewGroup parent) {

    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.store_home_gridview_row, null); 
    }

    TextView tv_title = (TextView) convertView.findViewById(R.id.textview_name);
    tv_title.setText("Title:"+MyArr.get(position).get("name"));

    TextView tv_artist = (TextView) convertView.findViewById(R.id.textview_artist);
    tv_artist.setText("Artist:"+MyArr.get(position).get("artist"));

    TextView tv_duration = (TextView) convertView.findViewById(R.id.textview_price);
    tv_duration.setText("Price:"+MyArr.get(position).get("price"));

    String abc = (MyArr.get(position).get("image"));
    String abcd = "http://192.168.1.132/onlinestore/images/products/"+abc;
    System.out.println("abcd" +abcd);

    //i can fetch the correct url in logcat but when i give to url
    new DownloadImageTask((ImageView) convertView.findViewById(R.id.imageView1))
    .execute(abcd); //image is not printing if i give the url which is stored in abcd
} }

/* 从 URL 获取 JSON 代码 */

 public String getJSONUrl(String url) {
    StringBuilder str = new StringBuilder();
    HttpClient client = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(url);
    try 
    {
        HttpResponse response = client.execute(httpGet);
        StatusLine statusLine = response.getStatusLine();
        int statusCode = statusLine.getStatusCode();
        System.out.println ( "status Code : " + statusCode );
        if (statusCode == 200) 
        { 
            HttpEntity entity = response.getEntity();
            InputStream content = entity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(content));
            String line;
            while ((line = reader.readLine()) != null) 
            {
                str.append(line);
            }
        }   
        else 
        {
            Log.e("Log", "Failed to download file..");
        }
    } 
    catch (ClientProtocolException e) 
    {
        e.printStackTrace();
    } 
    catch (IOException e) 
    {
        e.printStackTrace();
    }
    System.out.println ( "str : " + str.toString() );
    return str.toString();
}}

最佳答案

也许你应该使用一些很酷的库来完成你的图像任务。我推荐你使用很酷的异步图像库-------- Picasso 。有了这个库,您可以非常轻松地从网络获取图像。如:

Picasso.with(context).load("http://YOUR-IMAGE-URL").placeholder(YOUR-LOADING-IMAGE-_RESID).error(YOUR-ERROR-IMAGE-_RESID).into(imageView);`

关于java - ImageURL 未填充在 Android 中 imageView 内的图像中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23559669/

相关文章:

java - replace() 不适用于多 fragment

必须调用 Android requestFeature(),而不是对话框

java - 如何用Java从数据库中检索每一行和每一列的数据

java - 我可以在 JFrame 中嵌入 JavaFX 应用程序类吗?

Android:activity.getResources().getConfiguration().orientation 在 Samsung galaxy 设备上返回错误值

java - Android onActivityResult 在对话框中

java - 使用 Java 反射创建 eval() 方法

java - 我可以将序列化的java对象转换为文本吗?

android - Android应用程序中的离散搜索栏?

java - 安卓|如何在 ScrollView 中使用 ExpandableListView?