私はここでSOに関する同様の質問に答えました:https ://stackoverflow.com/a/10561377/450534
protected void uploadPhoto() {
if (!Utility.mFacebook.isSessionValid()) {
Util.showAlert(this, "Warning", "You must first log in.");
} else {
if (targetUri != null) {
Bundle params = new Bundle();
try {
params.putByteArray("photo", Utility.scaleImage(getApplicationContext(), targetUri));
} catch (Exception e) {
e.printStackTrace();
}
params.putString("caption", txtPhotoCaption.getText().toString());
Utility.mAsyncRunner.request( "replace this with your ID"+ "/photos&access_token=" + Utility.mFacebook.getAccessToken(),
params, "POST", new PhotoUploadListener(), null);
txtPhotoCaption.setText("");
Toast.makeText(getApplicationContext(), "Photo uploaded successfully", Toast.LENGTH_SHORT).show();
this.finish();
}
}
}
サンプルアプリで使用しているのと同じ方法を使用していることに注意してください。画像を拡大縮小したり拡大縮小したりしないことの長所と短所を理解することなく。「caption」属性はEditTextから取得されています。そして、実際の画像は、グローバル変数として宣言されているフィールド「 targetUri 」にあります。デバイスのデフォルトのギャラリーを使用して画像を取得しています。完全を期すために、これはデフォルトのギャラリーアプリから画像を取得するためのコードです。(これにより、ユーザーはアプリを選択するように求められます)
btnAddPhoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 0);
}
});
そしてそれはonActivityResult()
ここで処理されます:
注:ログインしたユーザーのプレビューとして、ImageViewで選択した画像も設定しています
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
Bitmap bitmap = null;
if (resultCode == RESULT_OK) {
targetUri = data.getData();
Log.e("IMG URI", targetUri.toString());
try {
bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(targetUri));
try {
Utility.scaleImage(getApplicationContext(), targetUri);
imgDisplaySelectedImage.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
言及されたリンクの答えは、友達の壁に写真をアップロードすることです。しかし、友達IDの代わりに私のIDを持つ同じコードは、基本的にあなたが望むのと同じタスクを実行します。これがあなたにも役立つことを願っています。
編集:最初のコードで、これを自分のIDに置き換えます。また、を呼び出すこともできます"me/photos&access_token="
。残りは同じままです。