Error uploading photos using Facebook SDK

2019-07-10 15:55发布

This is a similar problem to Intermittent SSL error when uploading photos using Facebook Android SDK 3.0 but that question has no responses and I'd like to provide more detail in the question.

I'm attempting to upload photos to a user's Facebook account using the following code:

ArrayList<Request> requests = new ArrayList<Request>();
for (String id : ids) {
    Uri uri = Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id);
    Bitmap b;
    try {
        b = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
    } catch (FileNotFoundException e) {
        Log.e(LOG_TAG, "Error uploading photo to facebook", e);
        return;
    } catch (IOException e) {
        Log.e(LOG_TAG, "Error uploading photo to facebook", e);
        return;
    }
    if (b != null) {
        requests.add(Request.newUploadPhotoRequest(FacebookLoginHelper.getInstance().getActiveSession(), b,
                null));
    }
}
List<Response> responses = Request.executeBatchAndWait(requests);
for (Response response : responses) {
    if (response.getError() == null) {
        Log.w(LOG_TAG, "Successfully uploaded image");
    } else {
        Log.w(LOG_TAG, "Error uploading image: " + response.getError().getErrorMessage());
    }
}

I'm receiving this error:

02-28 11:26:52.936: W/System.err(5848): javax.net.ssl.SSLException: Write error:        ssl=0x72be0428: I/O error during system call, Broken pipe
02-28 11:26:52.936: W/System.err(5848):     at org.apache.harmony.xnet.provider.jsse.NativeCrypto.SSL_write(Native Method)
02-28 11:26:52.936: W/System.err(5848):     at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl$SSLOutputStream.write(OpenSSLSocketImpl.java:706)
02-28 11:26:52.936: W/System.err(5848):     at libcore.net.http.ChunkedOutputStream.writeHex(ChunkedOutputStream.java:102)
02-28 11:26:52.936: W/System.err(5848):     at libcore.net.http.ChunkedOutputStream.writeBufferedChunkToSocket(ChunkedOutputStream.java:128)
02-28 11:26:52.946: W/System.err(5848):     at libcore.net.http.ChunkedOutputStream.write(ChunkedOutputStream.java:77)
02-28 11:26:52.946: W/System.err(5848):     at java.io.BufferedOutputStream.flushInternal(BufferedOutputStream.java:185)
02-28 11:26:52.946: W/System.err(5848):     at java.io.BufferedOutputStream.write(BufferedOutputStream.java:139)
02-28 11:26:52.946: W/System.err(5848):     at android.graphics.Bitmap.nativeCompress(Native Method)
02-28 11:26:52.946: W/System.err(5848):     at android.graphics.Bitmap.compress(Bitmap.java:875)
02-28 11:26:52.946: W/System.err(5848):     at com.facebook.Request$Serializer.writeBitmap(Request.java:1688)
02-28 11:26:52.946: W/System.err(5848):     at com.facebook.Request$Serializer.writeObject(Request.java:1666)
02-28 11:26:52.946: W/System.err(5848):     at com.facebook.Request.serializeAttachments(Request.java:1569)
02-28 11:26:52.946: W/System.err(5848):     at com.facebook.Request.serializeToUrlConnection(Request.java:1467)
02-28 11:26:52.946: W/System.err(5848):     at com.facebook.Request.toHttpConnection(Request.java:933)
02-28 11:26:52.946: W/System.err(5848):     at com.facebook.Request.executeBatchAndWait(Request.java:1027)
02-28 11:26:52.946: W/System.err(5848):     at com.facebook.Request.executeBatchAndWait(Request.java:1003)
02-28 11:26:52.946: W/System.err(5848):     at com.mycompany.mypackage.services.FacebookPhotoUploadService.onHandleIntent(FacebookPhotoUploadService.java:50)
02-28 11:26:52.946: W/System.err(5848):     at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
02-28 11:26:52.946: W/System.err(5848):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-28 11:26:52.946: W/System.err(5848):     at android.os.Looper.loop(Looper.java:137)
02-28 11:26:52.946: W/System.err(5848):     at android.os.HandlerThread.run(HandlerThread.java:60)
02-28 11:26:52.946: D/skia(5848): ------- write threw an exception
02-28 11:26:52.946: W/com.mycompany.mypackage.services.FacebookPhotoUploadService(5848): Error uploading image: com.facebook.FacebookException: could not construct request body

Things I'm sure of:

  • the bitmap I'm sending in the request is valid
  • I'm properly logged into Facebook with an active session

I was able to get this to work successfully twice out of about 15 or 20 tries (verified by going to Facebook in my browser and seeing the image there).

I don't think it's important, but this is happening in the onHandleIntent() method of an IntentService.

1条回答
淡お忘
2楼-- · 2019-07-10 16:43

I found a solution to this problem which involves bypassing the Facebook SDK and doing the HTTP call yourself. Details here: https://stackoverflow.com/a/15260387/758458

查看更多
登录 后发表回答