我希望从SD卡上传图片到脸谱。我尝试了很多,但我无法获得成功。我该怎么办?
我的代码是:
<tr>
Bundle params = new Bundle();
params.putString("message", "Uploaded on " + now());
params.putByteArray("picture", bytes); //bytes contains photo bytes, no problem here
asyncRunner.request("me/photos", params, "POST", new PostPhotoRequestListener(), null);
</tr>
答案 0 :(得分:2)
从此LINK复制解决方案。 您应该使用Facebook API http://developers.facebook.com/docs/guides/mobile/#android。然后使用此代码:
byte[] data = null;
try {
FileInputStream fis = new FileInputStream(PATH_TO_FILE);
Bitmap bi = BitmapFactory.decodeStream(fis);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bi.compress(Bitmap.CompressFormat.JPEG, 100, baos);
data = baos.toByteArray();
} catch (FileNotFoundException e) {
e.printStackTrace();
Log.d("onCreate", "debug error e = " + e.toString());
}
Bundle params = new Bundle();
params.putString("method", "photos.upload");
params.putByteArray("picture", data);
Facebook facebook = new Facebook(FACEBOOK_APP_ID);
AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook);
mAsyncRunner.request(null, params, "POST", new RequestListener() {
public void onMalformedURLException(MalformedURLException e, Object state) {
Log.d("request RequestListener", "debug onMalformedURLException");
}
public void onIOException(IOException e, Object state) {
Log.d("request RequestListener", "debug onIOException");
}
public void onFileNotFoundException(FileNotFoundException e, Object state) {
Log.d("request RequestListener", "debug onFileNotFoundException");
}
public void onFacebookError(FacebookError e, Object state) {
Log.d("request RequestListener", "debug onFacebookError");
}
public void onComplete(String response, Object state) {
Log.d("request RequestListener", "debug onComplete");
}
}, null);
请确保您的应用程序具有互联网和SD卡阅读权限
答案 1 :(得分:0)
我在这里回答了类似的问题: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();
}
}
}
请注意,我使用的方法与示例应用中使用的方法相同。没有涉及缩放或缩放图像的优点或缺点。正在从EditText中提取“标题”属性。实际图像位于“ targetUri ”字段中,该字段被声明为全局变量。它使用设备上的默认图库获取图像。为了完整起见,这是从默认的Gallery应用程序中获取图像的代码。 (这会提示用户选择应用程序)
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="
。其余的保持不变。