Search code examples
androidfacebook-wallfacebook-android-sdk

Error while publishing photo on facebook wall using android facebook sdk


I am using official android Facebook sdk for publishing photo on wall but it crashes after sign in while allowing app permission to my account. I am using android 3.1 library for this app.

following is the code which i am using to publish photo at wall,

facebook.authorize(ImageCropShare.this, new String[] { "publish_stream" },
                            new DialogListener() {

                        @Override
                        public void onFacebookError(FacebookError e) {
                        // TODO Auto-generated method stub

                        }

                        @Override
                        public void onError(DialogError dialogError) {
                        // TODO Auto-generated method stub

                        }

                        @Override
                        public void onComplete(Bundle values) {                                             

                                Bundle params = new Bundle();
                                params.putString(Facebook.TOKEN, values.getString(Facebook.TOKEN));
                                params.putByteArray("picture", data); // here data is byte array of bitmap image

                                try {
                                    facebook.request("me/photos", params, "POST");

                                } catch (FileNotFoundException fileNotFoundException) {
                                   // makeToast(fileNotFoundException.getMessage());
                                } catch (MalformedURLException malformedURLException) {
                                   // makeToast(malformedURLException.getMessage());
                                } catch (IOException ioException) {
                                   // makeToast(ioException.getMessage());
                                }

                                try {
                                    facebook.logout(getApplicationContext());
                                } catch (MalformedURLException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                } catch (IOException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }
                        }

                        @Override
                        public void onCancel() {
                        // TODO Auto-generated method stub
                            try {
                                facebook.logout(getApplicationContext());
                            } catch (MalformedURLException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                        }
                    });

following is the function by which i am getting byte array from bitmap image

public byte[] bitmapTobyte(Bitmap bitmap)
    {
        byte[] data = null;

        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
        data = stream.toByteArray();

        return data;
    }

and following is my error log

01-04 11:44:04.022: E/AndroidRuntime(480): FATAL EXCEPTION: main
01-04 11:44:04.022: E/AndroidRuntime(480): android.os.NetworkOnMainThreadException
01-04 11:44:04.022: E/AndroidRuntime(480):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1077)
01-04 11:44:04.022: E/AndroidRuntime(480):  at java.net.InetAddress.lookupHostByName(InetAddress.java:477)
01-04 11:44:04.022: E/AndroidRuntime(480):  at java.net.InetAddress.getAllByNameImpl(InetAddress.java:277)
01-04 11:44:04.022: E/AndroidRuntime(480):  at java.net.InetAddress.getAllByName(InetAddress.java:249)
01-04 11:44:04.022: E/AndroidRuntime(480):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:69)
01-04 11:44:04.022: E/AndroidRuntime(480):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:48)
01-04 11:44:04.022: E/AndroidRuntime(480):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection$Address.connect(HttpConnection.java:304)
01-04 11:44:04.022: E/AndroidRuntime(480):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:89)
01-04 11:44:04.022: E/AndroidRuntime(480):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHttpConnection(HttpURLConnectionImpl.java:292)
01-04 11:44:04.022: E/AndroidRuntime(480):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.makeConnection(HttpURLConnectionImpl.java:274)
01-04 11:44:04.022: E/AndroidRuntime(480):  at org.apache.harmony.luni.internal.net.www.protocol.https.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:447)
01-04 11:44:04.022: E/AndroidRuntime(480):  at org.apache.harmony.luni.internal.net.www.protocol.https.HttpsURLConnectionImpl$HttpsEngine.makeConnection(HttpsURLConnectionImpl.java:419)
01-04 11:44:04.022: E/AndroidRuntime(480):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:217)
01-04 11:44:04.022: E/AndroidRuntime(480):  at org.apache.harmony.luni.internal.net.www.protocol.https.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:177)
01-04 11:44:04.022: E/AndroidRuntime(480):  at com.facebook.android.Util.openUrl(Util.java:178)
01-04 11:44:04.022: E/AndroidRuntime(480):  at com.facebook.android.Facebook.request(Facebook.java:563)
01-04 11:44:04.022: E/AndroidRuntime(480):  at com.mazda.cx5app.imagecrop.ImageCropShare$2$1.onComplete(ImageCropShare.java:123)
01-04 11:44:04.022: E/AndroidRuntime(480):  at com.facebook.android.Facebook$1.onComplete(Facebook.java:308)
01-04 11:44:04.022: E/AndroidRuntime(480):  at com.facebook.android.FbDialog$FbWebViewClient.shouldOverrideUrlLoading(FbDialog.java:145)
01-04 11:44:04.022: E/AndroidRuntime(480):  at android.webkit.CallbackProxy.uiOverrideUrlLoading(CallbackProxy.java:222)
01-04 11:44:04.022: E/AndroidRuntime(480):  at android.webkit.CallbackProxy.handleMessage(CallbackProxy.java:322)
01-04 11:44:04.022: E/AndroidRuntime(480):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-04 11:44:04.022: E/AndroidRuntime(480):  at android.os.Looper.loop(Looper.java:132)
01-04 11:44:04.022: E/AndroidRuntime(480):  at android.app.ActivityThread.main(ActivityThread.java:4025)
01-04 11:44:04.022: E/AndroidRuntime(480):  at java.lang.reflect.Method.invokeNative(Native Method)
01-04 11:44:04.022: E/AndroidRuntime(480):  at java.lang.reflect.Method.invoke(Method.java:491)
01-04 11:44:04.022: E/AndroidRuntime(480):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
01-04 11:44:04.022: E/AndroidRuntime(480):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
01-04 11:44:04.022: E/AndroidRuntime(480):  at dalvik.system.NativeStart.main(Native Method)

Please help me to solve this bug thanks in advance..!


Solution

  • I solved my bug,

    I was giving <uses-sdk android:minSdkVersion="12" /> in my manifest file, At that time it was crash due to NetworkOnMainThreadException.

    After google it i found that this execption is only thrown for applications targeting the Honeycomb SDK or higher.

    So, i just changed my SDK to lower level api, e.g: <uses-sdk android:minSdkVersion="8" />, Now its working fine..

    But Still if we want to use Honeycomb or higher SDK then we need to perform all the network related task in separate thread (use handler,Async Task or runOnUiThread).

    Here is good example of handler & Async Task and also check how to use runOnUiThread