从facebook-Android上的SD卡上传图像
我想从Facebook上的SD卡上传一张图片。可以通过URL在Facebook上发布图像,但如果我从SD卡传递图像,则不会发布图像 我正在传递从facebook-Android上的SD卡上传图像,android,facebook,image,Android,Facebook,Image,我想从Facebook上的SD卡上传一张图片。可以通过URL在Facebook上发布图像,但如果我从SD卡传递图像,则不会发布图像 我正在传递byteArray而不是用于上传图像的字符串,但我仍然没有得到输出。谁能帮我解决这个问题 我的代码: byte[] data = null; params.putString(Facebook.TOKEN, facebook.getAccessToken()); params.putString("link", "h
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) {
}
}
尝试以下Facebook开源示例: 在本例中,在hackbook下,用于将照片从SD卡上传到Facebook (或) 请尝试使用此代码从SD卡将图像上载到facebook
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);
注意:必须为项目上的internet和SD卡读取清单xml文件设置权限从SD卡上的Filepath创建文件对象
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;
}
你可以发布logcat错误!你需要从链接上传图片吗?不。我想从SD卡上传图片@VenkyI正在关闭logcat中的VM@在eclipse中设置adb或在尝试后重新启动eclipse post logcat Error您为github发送的链接也会显示来自URL而非SD卡的图像。。公共静态最终字符串HACK_ICON_URL=“”;我收到错误06-18 17:42:49.911:E/AndroidRuntime(8647):java.lang.OutOfMemoryError:位图大小超过VM预算您可以使用opts.inSampleSize=2修复此问题;或opts.inSampleSize=4 BitmapFactory.Options opts=new BitmapFactory.Options();//opts.inJustDecodeBounds=true;选择inSampleSize=2;位图myBitmap=BitmapFactory.decodeFile(st_imagepath,opts);如果我写这个代码:位图位图;ByteArrayOutputStream bas=新的ByteArrayOutputStream();BitmapFactory.Options Options=新的BitmapFactory.Options();options.inSampleSize=3;位图=BitmapFactory.decodeFile(文件路径,选项);compress(bitmap.CompressFormat.PNG,100,baos);data=baos.toByteArray();paos.flush();baos.close();我没有得到虚拟机预算大小错误,但随后关闭虚拟机将在logcat中出现。让我们