There is an error trying to get the Android Facebook profile picture.

Asked 2 years ago, Updated 2 years ago, 25 views

             try{
                   URL image_value = new URL("https://graph.facebook.com/"+id+ "/picture?type=large");
                   Log.i(getPackageName(),"url : "+image_value);
                   Bitmap bmp = null;
                   //Error bmp = BitmapFactory.codeStream(image_value.openConnection().getInputStream());
                   Log.i(getPackageName(),"bitmap : "+bmp);
                   String temp = BitMapToString(bmp);
                   PreferencesUtil.setPreferences(getApplicationContext(),"profile",temp);
               } } catch (IOException e){
                   Log.e("Err : ","image download err: "+e.getMessage());
                   e.printStackTrace();
               }

LOGCAT

W/System.err: android.os.NetworkOnMainThreadException

W/System.err: at com.example.cracking.fb_processes.MainActivity$2$override.onCompleted(MainActivity.java:107)

Why does an error occur in this code? I'd appreciate it if you could tell me how to solve it

android java

2022-09-22 13:23

1 Answers

From Honeycomb onwards, the UI thread generates a NetworkOnMainThreadException on network requests. Therefore, the part of requesting an image over the network must be handled by a separate thread.

One of the simple ways to modify code is to use AsyncTask to request an image from the background thread and forward the result (bitmap) to the UI thread. Please refer to the link below to modify the code to use AsyncTask.


2022-09-22 13:23

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.