Никто не может исправить ошибку "Key access_token", ожидаемую в байтах [] при загрузке фотографий на Facebook. - PullRequest
3 голосов
/ 05 февраля 2012

Я пытаюсь загрузить изображение в Facebook. К сожалению, он не будет загружен, и я получаю это в своем журнале.

02-04 18:57:16.293: E/AndroidRuntime(462): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
02-04 19:33:42.622: D/Facebook-Util(596): POST URL: https://graph.facebook.com/https://graph.facebook.com/me/photos
02-04 19:33:42.632: W/Bundle(596): Key access_token expected byte[] but value was a java.lang.String.  The default value <null> was returned.
02-04 19:33:42.632: W/Bundle(596): Attempt to cast generated internal exception:
02-04 19:33:42.632: W/Bundle(596): java.lang.ClassCastException: java.lang.String
02-04 19:33:42.632: W/Bundle(596):  at android.os.Bundle.getByteArray(Bundle.java:1305)
02-04 19:33:42.632: W/Bundle(596):  at com.facebook.android.Util.openUrl(Util.java:155)
02-04 19:33:42.632: W/Bundle(596):  at com.facebook.android.Facebook.request(Facebook.java:717)
02-04 19:33:42.632: W/Bundle(596):  at com.facebook.android.AsyncFacebookRunner$2.run(AsyncFacebookRunner.java:253)
02-04 19:33:42.632: W/Bundle(596): Key format expected byte[] but value was a java.lang.String.  The default value <null> was returned.
02-04 19:33:42.642: W/Bundle(596): Attempt to cast generated internal exception:
02-04 19:33:42.642: W/Bundle(596): java.lang.ClassCastException: java.lang.String
02-04 19:33:42.642: W/Bundle(596):  at android.os.Bundle.getByteArray(Bundle.java:1305)
02-04 19:33:42.642: W/Bundle(596):  at com.facebook.android.Util.openUrl(Util.java:155)
02-04 19:33:42.642: W/Bundle(596):  at com.facebook.android.Facebook.request(Facebook.java:717)
02-04 19:33:42.642: W/Bundle(596):  at com.facebook.android.AsyncFacebookRunner$2.run(AsyncFacebookRunner.java:253)
02-04 19:33:42.672: W/Bundle(596): Key method expected byte[] but value was a java.lang.String.  The default value <null> was returned.
02-04 19:33:42.672: W/Bundle(596): Attempt to cast generated internal exception:
02-04 19:33:42.672: W/Bundle(596): java.lang.ClassCastException: java.lang.String
02-04 19:33:42.672: W/Bundle(596):  at android.os.Bundle.getByteArray(Bundle.java:1305)
02-04 19:33:42.672: W/Bundle(596):  at com.facebook.android.Util.encodePostBody(Util.java:63)
02-04 19:33:42.672: W/Bundle(596):  at com.facebook.android.Util.openUrl(Util.java:182)
02-04 19:33:42.672: W/Bundle(596):  at com.facebook.android.Facebook.request(Facebook.java:717)
02-04 19:33:42.672: W/Bundle(596):  at com.facebook.android.AsyncFacebookRunner$2.run(AsyncFacebookRunner.java:253)
02-04 19:33:42.672: W/Bundle(596): Key access_token expected byte[] but value was a java.lang.String.  The default value <null> was returned.
02-04 19:33:42.683: W/Bundle(596): Attempt to cast generated internal exception:
02-04 19:33:42.683: W/Bundle(596): java.lang.ClassCastException: java.lang.String
02-04 19:33:42.683: W/Bundle(596):  at android.os.Bundle.getByteArray(Bundle.java:1305)
02-04 19:33:42.683: W/Bundle(596):  at com.facebook.android.Util.encodePostBody(Util.java:63)
02-04 19:33:42.683: W/Bundle(596):  at com.facebook.android.Util.openUrl(Util.java:182)
02-04 19:33:42.683: W/Bundle(596):  at com.facebook.android.Facebook.request(Facebook.java:717)
02-04 19:33:42.683: W/Bundle(596):  at com.facebook.android.AsyncFacebookRunner$2.run(AsyncFacebookRunner.java:253)
02-04 19:33:42.692: W/Bundle(596): Key format expected byte[] but value was a java.lang.String.  The default value <null> was returned.
02-04 19:33:42.692: W/Bundle(596): Attempt to cast generated internal exception:
02-04 19:33:42.692: W/Bundle(596): java.lang.ClassCastException: java.lang.String
02-04 19:33:42.692: W/Bundle(596):  at android.os.Bundle.getByteArray(Bundle.java:1305)
02-04 19:33:42.692: W/Bundle(596):  at com.facebook.android.Util.encodePostBody(Util.java:63)
02-04 19:33:42.692: W/Bundle(596):  at com.facebook.android.Util.openUrl(Util.java:182)
02-04 19:33:42.692: W/Bundle(596):  at com.facebook.android.Facebook.request(Facebook.java:717)
02-04 19:33:42.692: W/Bundle(596):  at com.facebook.android.AsyncFacebookRunner$2.run(AsyncFacebookRunner.java:253)
02-04 19:33:42.733: D/dalvikvm(596): GC_CONCURRENT freed 1301K, 59% free 2794K/6727K, external 4949K/5993K, paused 4ms+4ms

Вот метод, который я создал для загрузки.

public void uploadPhoto(String path){
    if(facebook.isSessionValid()){
        String accessToken = facebook.getAccessToken();
        byte[] access_token = accessToken.getBytes(); 
        byte[] data = null; 

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = getScaleFactor(path);

        Bitmap bi = BitmapFactory.decodeFile(path, options);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bi.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        data = baos.toByteArray();

        Bundle params = new Bundle();
        params.putByteArray("access_token", access_token);
        //params.putByteArray("method", "photos.upload".getBytes());
        params.putByteArray("photo", data);

        AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook);
        mAsyncRunner.request("https://graph.facebook.com/me/photos", params, "POST", new UploadListener(), null);
    }
    else{
        showToast("Session is not valid.");
    }
}

/** Scales images to prep for upload */
public int getScaleFactor(String path){
    // Decode image size
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(path, o);

    // The new size we want to scale to
    final int REQUIRED_SIZE = 70;

    // Find the correct scale value. It should be power of 2.
    int scale = 1;
    while(o.outWidth/scale/2 >= REQUIRED_SIZE && o.outHeight/scale/2 >= REQUIRED_SIZE){
        scale *= 2;
    }
    System.out.println(scale);
    return scale;
}
...