我一直在寻找在Facebook墙上发布图像,从早上开始用谷歌搜索,得到了很多关于发布的信息,但我没有运气。我可以发布链接没有任何错误,但一旦我尝试图像获取NullPointer异常
在我的按钮单击事件监听器后,我调用此方法。如果我取消注释链接参数,那么它将显示一个用于询问共享和取消的对话框。如果我将params.putByteArray("picture", data);
这个代码放入,我将得到以下logcat错误
09-15 15:51:50.647: E/AndroidRuntime(9716): FATAL EXCEPTION: main
09-15 15:51:50.647: E/AndroidRuntime(9716): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.m2fpremiun.dressup/com.m2fpremiun.dressup.FBProjectActivity}: java.lang.NullPointerException
09-15 15:51:50.647: E/AndroidRuntime(9716): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2189)
09-15 15:51:50.647: E/AndroidRuntime(9716): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2216)
09-15 15:51:50.647: E/AndroidRuntime(9716): at android.app.ActivityThread.access$600(ActivityThread.java:149)
09-15 15:51:50.647: E/AndroidRuntime(9716): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1305)
09-15 15:51:50.647: E/AndroidRuntime(9716): at android.os.Handler.dispatchMessage(Handler.java:99)
09-15 15:51:50.647: E/AndroidRuntime(9716): at android.os.Looper.loop(Looper.java:153)
09-15 15:51:50.647: E/AndroidRuntime(9716): at android.app.ActivityThread.main(ActivityThread.java:5000)
09-15 15:51:50.647: E/AndroidRuntime(9716): at java.lang.reflect.Method.invokeNative(Native Method)
09-15 15:51:50.647: E/AndroidRuntime(9716): at java.lang.reflect.Method.invoke(Method.java:511)
09-15 15:51:50.647: E/AndroidRuntime(9716): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
09-15 15:51:50.647: E/AndroidRuntime(9716): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
09-15 15:51:50.647: E/AndroidRuntime(9716): at dalvik.system.NativeStart.main(Native Method)
09-15 15:51:50.647: E/AndroidRuntime(9716): Caused by: java.lang.NullPointerException
09-15 15:51:50.647: E/AndroidRuntime(9716): at libcore.net.UriCodec.encode(UriCodec.java:132)
09-15 15:51:50.647: E/AndroidRuntime(9716): at java.net.URLEncoder.encode(URLEncoder.java:50)
09-15 15:51:50.647: E/AndroidRuntime(9716): at com.facebook.android.Util.encodeUrl(Util.java:85)
09-15 15:51:50.647: E/AndroidRuntime(9716): at com.facebook.android.Facebook.dialog(Facebook.java:774)
09-15 15:51:50.647: E/AndroidRuntime(9716): at com.m2fpremiun.dressup.FBProjectActivity.postToWall(FBProjectActivity.java:436)
09-15 15:51:50.647: E/AndroidRuntime(9716): at com.m2fpremiun.dressup.FBProjectActivity.loginToFacebook(FBProjectActivity.java:288)
09-15 15:51:50.647: E/AndroidRuntime(9716): at com.m2fpremiun.dressup.FBProjectActivity.onCreate(FBProjectActivity.java:83)
09-15 15:51:50.647: E/AndroidRuntime(9716): at android.app.Activity.performCreate(Activity.java:5020)
09-15 15:51:50.647: E/AndroidRuntime(9716): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
09-15 15:51:50.647: E/AndroidRuntime(9716): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2153)
09-15 15:51:50.647: E/AndroidRuntime(9716): ... 11 more
请有人帮我解决这个问题。
public void postToWall() {
// post on user's wall.
byte[] data = null;
Bundle params = new Bundle();
//postParams.putString("description","I'm playing JackPac and Scored "+hScore+" ! Can you beat my High Score?");
// postParams.putString("link", "https://play.google.com/store/apps/details?id=com.mobi2fun.jackpac&hl=en");
Bitmap bi = BitmapFactory.decodeFile("/storage/sdcard0/Game/test.png");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bi.compress(Bitmap.CompressFormat.PNG, 100, baos);
data = baos.toByteArray();
params.putString("caption", "Image Post");
//params.putString("description","I'm playing XYZ Game and Scored "+hScore+" ! Can you beat my High Score?");
//params.putString("link", "https://play.google.com/store/apps/details?id=xyz.package&hl=en");
params.putString("method", "photos.upload");
params.putByteArray("picture", data);
facebook.dialog(this, "feed",params, new DialogListener() {
@Override
public void onFacebookError(FacebookError e) {
}
@Override
public void onError(DialogError e) {
}
@Override
public void onComplete(Bundle values) {
logoutFromFacebook();
try {
//facebook.logout(getApplicationContext());
//facebook.setAccessExpires(0);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void onCancel() {
logoutFromFacebook();
try {
// facebook.logout(getApplicationContext());
mAsyncRunner.logout(getApplicationContext(),new RequestListener() {
@Override
public void onMalformedURLException(MalformedURLException e, Object state) {
// TODO Auto-generated method stub
}
@Override
public void onIOException(IOException e, Object state) {
// TODO Auto-generated method stub
}
@Override
public void onFileNotFoundException(FileNotFoundException e, Object state) {
// TODO Auto-generated method stub
}
@Override
public void onFacebookError(FacebookError e, Object state) {
// TODO Auto-generated method stub
}
@Override
public void onComplete(String response, Object state) {
// TODO Auto-generated method stub
}
});
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
我在清单文件中给出的这些权限
我检查了这个链接并尝试了所有代码但没有运气Facebook wall post with text and image Upload image to FaceBook from SDCard
答案 0 :(得分:0)
我在Facebook墙上使用此代码分享文字和图片。它的作品。它可能对你有所帮助。试试这个。
从你的oncreate方法调用facebooksharing方法,如下面的代码所示。你可以从你想要的地方打电话。
CallbackManager callbackManager;
private LoginManager manager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(this.getApplicationContext());
setContentView(R.layout.Your_layout);
callbackManager = CallbackManager.Factory.create();
//call method for sharing post
FaceBookSharing();
}
public void FaceBookSharing() {
List<String> permissionNeeds = Arrays.asList("publish_actions");
manager = LoginManager.getInstance();
manager.logInWithPublishPermissions(this, permissionNeeds);
manager.registerCallback(callbackManager,
new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
PostImage();
}
@Override
public void onCancel() {
System.out.println("onCancel");
}
@Override
public void onError(FacebookException exception) {
System.out.println("onError");
}
});
}
private void PostImage() {
Bitmap image = BitmapFactory.decodeResource(getResources(),
R.drawable.ic_launcher);
SharePhoto photo = new SharePhoto.Builder().setBitmap(image)
.setCaption("Hey this is Demo post!")
.build();
SharePhotoContent content = new SharePhotoContent.Builder().addPhoto(
photo).build();
ShareApi.share(content, null);
Toast.makeText(this, "Succsesfully posted on your wall", Toast.LENGTH_LONG)
.show();
}
&#13;