Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8287023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T11:55:35+00:00 2026-06-08T11:55:35+00:00

im trying to upload images to facebook. The code i have now is working.

  • 0

im trying to upload images to facebook. The code i have now is working. I am uploading an image from my database using the images path.

    public void postImage() {

    facebook.authorize(this, new DialogListener() {
        public void onComplete(Bundle values) {
             byte[] data = null;
             String upploadpath = ((MyVariables) getApplication()) .getUpploadPath();

             Bitmap bi = BitmapFactory.decodeFile(upploadpath);
             Bitmap scaledimage = Bitmap.createScaledBitmap(bi, 720, 520, false);

             ByteArrayOutputStream baos = new ByteArrayOutputStream();
             scaledimage.compress(Bitmap.CompressFormat.JPEG, 100, baos);
             data = baos.toByteArray();

             Bundle params = new Bundle();
             params.putString(Facebook.TOKEN, facebook.getAccessToken());
             params.putString("method", "photos.upload");
             params.putByteArray("picture", data);
             params.putString("caption", "Caption text");



             AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook);
             mAsyncRunner.request(null, params, "POST", new PhotoUploadListener(), null);
        }

        public void onFacebookError(FacebookError error) {}

        public void onError(DialogError e) {}

        public void onCancel() {}         
    });
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    facebook.authorizeCallback(requestCode, resultCode, data);
}

/*
 * callback for the photo upload
 */
public class PhotoUploadListener extends BaseRequestListener {

    @Override
    public void onComplete(final String response, final Object state) {
        Log.e("Facebook", "onComplete");

    }

    public void onFacebookError(FacebookError error) {

    }
}

The problem here is that i hardcoded the resize of the image. and the orientation is not working, so the pictures get uploaded sideways even though the picture is taken in portrait mode.

using facebooks hackbook, the images get rescaled and correct orientation. i have tried to use this code instead:

 public void postImage() {

    facebook.authorize(this, new DialogListener() {
        public void onComplete(Bundle values) {

             String upploadpath = ((MyVariables) getApplication()) .getUpploadPath();
             Uri photoUri = Uri.parse(upploadpath);

             if (photoUri != null) {
                 Bundle params = new Bundle();
                 try {
                     params.putByteArray("photo",
                             Utility.scaleImage(getApplicationContext(), photoUri));
                 } catch (IOException e) {
                     e.printStackTrace();
                 }
                 params.putString("caption", "FbAPIs Sample App photo upload");

 Utility.mAsyncRunner.request("me/photos", params, "POST",
                         new PhotoUploadListener(), null);
             } else {
                 Toast.makeText(getApplicationContext(),
                         "Error selecting image from the gallery.", Toast.LENGTH_SHORT)
                         .show();
             }
        }

        public void onFacebookError(FacebookError error) {}

        public void onError(DialogError e) {}

        public void onCancel() {}         
    });
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    facebook.authorizeCallback(requestCode, resultCode, data);
}

/*
 * callback for the photo upload
 */
public class PhotoUploadListener extends BaseRequestListener {

    @Override
    public void onComplete(final String response, final Object state) {
        Log.e("Facebook", "onComplete");

    }

    public void onFacebookError(FacebookError error) {

    }
}

but this gives me the error:

 07-23 18:47:22.290: E/LOG_TAG(9293): Setting filepath: /mnt/sdcard/Pictures/MyApp/IMG_20120721_192609.jpg
 07-23 18:47:27.340: E/AndroidRuntime(9293): FATAL EXCEPTION: main
 07-23 18:47:27.340: E/AndroidRuntime(9293): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=32665, result=-1, data=Intent { (has extras) }} to activity {com.ubbep.myapp/com.ubbep.myapp.ViewContact}: java.lang.NullPointerException
 07-23 18:47:27.340: E/AndroidRuntime(9293):    at android.app.ActivityThread.deliverResults(ActivityThread.java:2992)
 07-23 18:47:27.340: E/AndroidRuntime(9293):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3035)
 07-23 18:47:27.340: E/AndroidRuntime(9293):    at android.app.ActivityThread.access$1100(ActivityThread.java:127)
 07-23 18:47:27.340: E/AndroidRuntime(9293):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1189)
 07-23 18:47:27.340: E/AndroidRuntime(9293):    at android.os.Handler.dispatchMessage(Handler.java:99)
 07-23 18:47:27.340: E/AndroidRuntime(9293):    at android.os.Looper.loop(Looper.java:137)
 07-23 18:47:27.340: E/AndroidRuntime(9293):    at android.app.ActivityThread.main(ActivityThread.java:4507)
 07-23 18:47:27.340: E/AndroidRuntime(9293):    at java.lang.reflect.Method.invokeNative(Native Method)
 07-23 18:47:27.340: E/AndroidRuntime(9293):    at java.lang.reflect.Method.invoke(Method.java:511)
 07-23 18:47:27.340: E/AndroidRuntime(9293):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
 07-23 18:47:27.340: E/AndroidRuntime(9293):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
 07-23 18:47:27.340: E/AndroidRuntime(9293):    at dalvik.system.NativeStart.main(Native Method)
 07-23 18:47:27.340: E/AndroidRuntime(9293): Caused by: java.lang.NullPointerException
 07-23 18:47:27.340: E/AndroidRuntime(9293):    at com.ubbep.myapp.ViewContact$3.onComplete(ViewContact.java:360)
 07-23 18:47:27.340: E/AndroidRuntime(9293):    at com.facebook.android.Facebook.authorizeCallback(Facebook.java:433)
 07-23 18:47:27.340: E/AndroidRuntime(9293):    at com.ubbep.myapp.ViewContact.onActivityResult(ViewContact.java:381)
 07-23 18:47:27.340: E/AndroidRuntime(9293):    at android.app.Activity.dispatchActivityResult(Activity.java:4649)
 07-23 18:47:27.340: E/AndroidRuntime(9293):    at android.app.ActivityThread.deliverResults(ActivityThread.java:2988)

so by removing:

  facebook.authorizeCallback(requestCode, resultCode, data);

from:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    facebook.authorizeCallback(requestCode, resultCode, data);
}

I get no errors, but the picture is not uploaded.

The facebook hackbook Utility.java has this code in it:

   public static byte[] scaleImage(Context context, Uri photoUri) throws IOException {
    InputStream is = context.getContentResolver().openInputStream(photoUri);
    BitmapFactory.Options dbo = new BitmapFactory.Options();
    dbo.inJustDecodeBounds = true;
    BitmapFactory.decodeStream(is, null, dbo);
    Log.e("scaleImage", "scaling image" + photoUri);
    is.close();

    int rotatedWidth, rotatedHeight;
    int orientation = getOrientation(context, photoUri);

    if (orientation == 90 || orientation == 270) {
        rotatedWidth = dbo.outHeight;
        rotatedHeight = dbo.outWidth;
    } else {
        rotatedWidth = dbo.outWidth;
        rotatedHeight = dbo.outHeight;
    }

    Bitmap srcBitmap;
    is = context.getContentResolver().openInputStream(photoUri);
    if (rotatedWidth > MAX_IMAGE_DIMENSION || rotatedHeight > MAX_IMAGE_DIMENSION) {
        float widthRatio = ((float) rotatedWidth) / ((float) MAX_IMAGE_DIMENSION);
        float heightRatio = ((float) rotatedHeight) / ((float) MAX_IMAGE_DIMENSION);
        float maxRatio = Math.max(widthRatio, heightRatio);
        Log.e("Bitmap srcBitmap", "srcBitmap");
        // Create the bitmap from file
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = (int) maxRatio;
        srcBitmap = BitmapFactory.decodeStream(is, null, options);
        Log.e("Create Bitmap", "Creating new bitmap");
    } else {
        srcBitmap = BitmapFactory.decodeStream(is);
    }
    is.close();

    /*
     * if the orientation is not 0 (or -1, which means we don't know), we
     * have to do a rotation.
     */
    if (orientation > 0) {
        Matrix matrix = new Matrix();
        matrix.postRotate(orientation);

        srcBitmap = Bitmap.createBitmap(srcBitmap, 0, 0, srcBitmap.getWidth(),
                srcBitmap.getHeight(), matrix, true);
    }

    String type = context.getContentResolver().getType(photoUri);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    if (type.equals("image/png")) {
        srcBitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
    } else if (type.equals("image/jpg") || type.equals("image/jpeg")) {
        srcBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    }
    byte[] bMapArray = baos.toByteArray();
    baos.close();
    return bMapArray;
}

public static int getOrientation(Context context, Uri photoUri) {
    /* it's on the external media. */
    Cursor cursor = context.getContentResolver().query(photoUri,
            new String[] { MediaStore.Images.ImageColumns.ORIENTATION }, null, null, null);

    if (cursor.getCount() != 1) {
        return -1;
    }

    cursor.moveToFirst();
    return cursor.getInt(0);
}
 }

So what am i doing wrong, i have tried alot of diferent solutions on the net, but nothing is working. 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T11:55:36+00:00Added an answer on June 8, 2026 at 11:55 am

    The orientation of an image is stored as EXIF data. It doesn’t look like you are copying the EXIF data from the original bitmap to the scaled bitmap.

    You can use the ExifInterface to copy the relevant information from the old bitmap to the new one.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to upload an image to facebook using the iOS SDK from Facebook.
i am trying to upload an image from a jsp page using servlet. but
I am trying to upload images using a webservice from silverlight. I am first
I'm trying to upload images (byte arrays) from the client to a database -
I'm currently trying to upload an image from my Mac application to Facebook. To
I am trying to let the user upload an image from my facebook app
I am trying to upload a local image to Facebook from my native Android
I am trying to upload multiple images and I have a jquery plugin set
I'm trying to put together an image uploader. I want to upload images into
I'm trying to upload an image file from an android device to my drupal

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.