我想从 SD 卡上传一张图片到 Facebook。可以通过 URL 在 Facebook 上发布图片,但如果我从 SD 卡传递图片,则不会发布。
我正在传递 byteArray
而不是用于上传图像的字符串,但我仍然没有得到输出。谁能帮我解决这个问题?
我的代码:
byte[] data = null;
params.putString(Facebook.TOKEN, facebook.getAccessToken());
params.putString("link", "https://www.facebook.com/pages/My-Short-Sale-Score/242779072468511");
params.putByteArray("picture",data);
Facebook facebook = new Facebook("318633718220473");
mAsyncRunner = new AsyncFacebookRunner(facebook);
mAsyncRunner.request(null, params, "POST", new SampleUploadListener(), null);
facebook.dialog(this, "stream.publish", params,
new DialogListener() {
@Override
public void onComplete(Bundle values){}
@Override
public void onFacebookError(FacebookError error) {}
@Override
public void onError(DialogError e) {}
@Override
public void onCancel() {}
}
);
}
public class SampleUploadListener extends BaseRequestListener {
public void onComplete(final String response, final Object state) {
try {
// process the response here: (executed in background thread)
Log.d("Facebook-Example", "Response: " + response.toString());
JSONObject json = Util.parseJson(response);
final String src = json.getString("src");
// then post the processed result back to the UI thread
// if we do not do this, an runtime exception will be generated
// e.g. "CalledFromWrongThreadException: Only the original
// thread that created a view hierarchy can touch its views."
} catch (JSONException e) {
Log.w("Facebook-Example", "JSON Error in response");
} catch (FacebookError e) {
Log.w("Facebook-Example", "Facebook Error: " + e.getMessage());
}
}
@Override
public void onFacebookError(FacebookError e, Object state) {
}
}
最佳答案
从 SD 卡上的文件路径创建一个文件对象,例如
File ff = new File("Your sdcard file path");
bb = decode(ff);
}
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bb.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
Bundle params = new Bundle();
params.putString("method", "photos.upload");
params.putString("caption", msg);
params.putByteArray("image", byteArray);
try {
facebook.request("me/feed", params, "POST");
} catch (FileNotFoundException fileNotFoundException) {
fileNotFoundException.printStackTrace();
} catch (MalformedURLException malformedURLException) {
malformedURLException.printStackTrace();
} catch (IOException ioException) {
ioException.printStackTrace();
}
AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook);
mAsyncRunner.request(null, params, "POST", new SampleUploadListener(),
null);
// code for decode method
private Bitmap decode(File f) {
try {
//Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f),null,o);
//The new size we want to scale to
final int REQUIRED_SIZE=70;
//Find the correct scale value. It should be the power of 2.
int scale=1;
while(o.outWidth/scale/2>=REQUIRED_SIZE && o.outHeight/scale/2>=REQUIRED_SIZE)
scale*=2;
//Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize=scale;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {}
return null;
}
关于android - 从 SD 卡上传图片到 Facebook - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11080443/