从URL加载图像时出现网络错误

时间:2014-10-30 12:56:30

标签: android google-maps google-maps-markers google-maps-api-2

我正在从将其转换为位图的URL加载图像,然后将其添加到地图中的标记。我有两个标记,一个用于我的个人资料,另一个用于朋友个人资料。

当我加载图像时,我收到以下错误,图像没有加载。没有力量关闭。

 android.os.NetworkOnMainThreadException

我不确定这是什么类型的错误。

以下是我正在尝试的代码:以下相同代码适用于我的个人资料照片。只有在我尝试加载好友列表时才会出现此问题。

我正在调用下面的内部循环。

for (int i = 0; i < friendsList.size(); i++)
{
    userName = friendsList.get(i).getName();
    lati = friendsList.get(i).getLati();
    longi = friendsList.get(i).getLongi();
    photoUrl = friendsList.get(i).getImage();

    loadMyFriendsLocation(Double.parseDouble(lati), Double.parseDouble(longi), userName, photoUrl, 15);
}

loadmyfriendsloaction方法具有以下内容:

 View marker =  getActivity().getLayoutInflater().inflate(R.layout.activity_myfriendslocationmap, null);
 ImageView friendImageView = (ImageView) marker.findViewById(R.id.badge);

        try 
        {
            InputStream in = new java.net.URL(urlfriends).openStream();
            Bitmap mIcon11 = BitmapFactory.decodeStream(in);
            friendImageView.setImageBitmap(mIcon11);
        }
        catch (Exception e) 
        {
            Log.e("Error", "Message"+e);
            e.printStackTrace();
        }

        //TO CONVERT A VIEW INTO BITMAP
        marker.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),  MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
        marker.layout(0, 0, marker.getMeasuredWidth(),marker.getMeasuredHeight());
        final Bitmap icon = Bitmap.createBitmap(marker.getMeasuredWidth(), marker.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(icon);
        marker.draw(canvas);

        MarkerOptions markerOptionsforfriends = new MarkerOptions().position(position).icon(BitmapDescriptorFactory.fromBitmap(icon)).title(uname).anchor(0.5f, 1f);
        markerforfriends = googleMap.addMarker(markerOptionsforfriends);

我在这里完全空白我不知道这可能出错的地方,因为我的个人资料图片有相同的代码,但朋友的照片没有出现......

有人可以帮我解决这个问题吗?

谢谢!

2 个答案:

答案 0 :(得分:3)

当应用程序尝试在其主线程上执行网络操作时,抛出此异常。所以必须在下面使用: -

  1. 线程

    Thread thread = new Thread(new Runnable(){
        @Override
        public void run() {
            try {
                //Your code goes here
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
    
    thread.start(); 
    
  2. 的AsyncTask

    private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... urls) {
      //your code goes here
      return response;
    }
    
    @Override
    protected void onPostExecute(String result) {
      textView.setText(result);
    }
    

    }

  3. StrictMode.ThreadPolicy

    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    
    StrictMode.setThreadPolicy(policy); 
    

答案 1 :(得分:0)

如果您从网络或数据库中获取图像,则可以通过这种方式获取

来自网络

try { 
       Bitmap bitmap=null; 
       URL imageUrl = new URL(url); 
       HttpURLConnection conn = (HttpURLConnection)imageUrl.openConnection(); 
       conn.setConnectTimeout(30000); 
       conn.setReadTimeout(30000); 
       conn.setInstanceFollowRedirects(true); 
       InputStream is=conn.getInputStream(); 
       OutputStream os = new FileOutputStream(f); 
       Utils.CopyStream(is, os); 
       os.close(); 
       bitmap = decodeFile(f); 
       return bitmap; 
 } catch (Exception ex){ 
 ex.printStackTrace(); 
 return null; 
 } 
 }