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) {
}
}
ここでの問題は、画像のサイズ変更をハードコーディングしたことです。向きが機能していないため、写真が縦向きモードで撮影されていても、写真は横向きにアップロードされます。
Facebookのハックブックを使用すると、画像が再スケーリングされ、正しい向きになります。代わりにこのコードを使用しようとしました:
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 ハックブックの 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);
}
}
だから私は何を間違っているのですか、私はネット上でさまざまなソリューションをたくさん試しましたが、何も機能していません。:)