我试图将图像上传到Facebook。我现在的代码正在运行。我正在使用图像路径从我的数据库上传图像。
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) {
}
}
这里的问题是我硬编码了图像的大小调整。并且方向不起作用,因此即使照片是以纵向模式拍摄的,图片也会侧向上传。
使用脸谱黑客书,图像得到重新调整和正确的方向。我尝试使用此代码:
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) {
}
}
但这给了我错误:
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)
所以删除:
facebook.authorizeCallback(requestCode, resultCode, data);
来自:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
facebook.authorizeCallback(requestCode, resultCode, data);
}
我没有错误,但图片没有上传。
facebook hackbook Utility.java中包含以下代码:
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);
}
}
所以我做错了什么,我在网上尝试了很多不同的解决方案,但没有任何效果。 :)