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. 🙂
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.