これは私がアプリで使用するものです。そして、それは機能します。
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 the users 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();
}
}
}
サンプルアプリと同じ方法を使用していることに注意してください。画像のスケーリングまたはスケーリングしないことの長所または短所に入ることなく。「キャプション」属性は、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();
}
}
}
ここにコードを回答として投稿する前に、友人の壁へのアップロードを再度テストしました。そして、それは本当にうまくいきます。これがあなたにも役立つことを願っています。
編集:
最新のコメントで述べたように、Intent.ACTION_GET_CONTENT を使用すると、onActivityResult() の下に表示されるプレビュー イメージがうまく機能しませんでした。コードを少し変更するとうまくいきました。
私が奇妙だと思うのは、「Complete action using」ポップアップで、Galaxy S で Astro File Manager も Gallery も取得できなかったという事実です。これにより、File Manager (在庫ではなく市場外) と Quickoffice Pro が提供されます。 . そうは言っても、2 つのいずれかからの選択は引き続き機能し、写真は引き続きアップロードされます。