-1

I am loading image from an URL converting it to Bitmap and then adding that to marker in maps. I have two markers one for my profile and other for friends profile.

When I load the image I get the following error and the image doesn't get loaded. No Force Close too.

 android.os.NetworkOnMainThreadException

I am not sure what kind of error is this.

Here is the code that I am trying: The same below code works fine for my profile pic. This comes up only when I try to load my friends list.

I am calling the below inside for loop.

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);
}

And the loadmyfriendsloaction method has the following:

 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);

I am totally blank here I don't know where this could go wrong because my profile picture which has the same code comes up but the friends pics don't come up...

Can somebody help me fix this issue?

Thanks!

TheDevMan
  • 5,914
  • 12
  • 74
  • 144

2 Answers2

3

This exception is thrown when an application attempts to perform a networking operation on its main thread. So have to use below :-

  1. Thread

    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); 
    
duggu
  • 37,851
  • 12
  • 116
  • 113
0

If you fetch images from web or databse then you can fetch in this way

from web

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; 
 } 
 } 
Dilip
  • 2,622
  • 1
  • 20
  • 27