附加图像缺少共享到 Facebook
Attach image is misssing sharing to facebook
我正在制作一个 android 应用程序,对于这个应用程序,我需要将图片分享到 Facebook。我已经按照 facebook 共享网站上的所有程序进行操作。但是在单击共享按钮时它显示图像但在共享图像时丢失了。我的详细代码是-
public class ActivityShare extends Activity {
String[] ShareOption; //create a shareOption array
Spinner ShareOptionList; // define A spinner list to choose the score type
int driverStarScore = 4; // define a drivingstar score to give the star to a driver .
Facebook fb; // define facebook
SharedPreferences sp;
ImageView pic;
ListView list; // define list
String[] ShareItemName ={ //create a string array of shareItem name
"Dropbox",
"Email",
"Facebook",
"Google Plus",
"Twitter",
"Whatsapp",
};
Integer[] ShareImageId={ //create an integer array of images of the shareitem
R.drawable.ic_dropbox,
R.drawable.ic_email,
R.drawable.ic_facebook,
R.drawable.ic_googleplus,
R.drawable.ic_twitter,
R.drawable.ic_whatsapp,
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_share);
sp=getPreferences(MODE_PRIVATE);
String access_token= sp.getString("access_token", null);
long expires=sp.getLong("access_expires", 0);
if(access_token!=null){
fb.setAccessToken(access_token);
}
if(expires !=0){
fb.setAccessExpires(expires);
}
String APP_ID=getString(R.string.APP_ID);
fb=new Facebook(APP_ID);
ShareOptionList=(Spinner) findViewById(R.id.spinner_ShareScore);
ShareOption=getResources().getStringArray(R.array.ShareChooseOption);
ArrayAdapter<String> adapter1=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,ShareOption);
ShareOptionList.setAdapter(adapter1);
ShareOptionList.setOnItemSelectedListener(new OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
int index=arg0.getSelectedItemPosition();
Toast.makeText(getBaseContext(), "You select "+ ShareOption[index],Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
ShareListAdapter adapter=new ShareListAdapter(this, ShareItemName, ShareImageId);
list=(ListView)findViewById(R.id.listview_share);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
@SuppressWarnings("deprecation")
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
String Selecteditem= ShareItemName[+position]; //response to click on the list
Toast.makeText(ActivityShare.this, Selecteditem, Toast.LENGTH_SHORT).show();
if(Selecteditem=="Facebook"){ //if facebook item is pressed
Bundle params=new Bundle();
params.putString("name", "ASSES ME");
params.putString("caption", "My Driving Score");
params.putString("link", "http://www.mybringback.com/"); //the link of the app
if (driverStarScore == 1){ // if someone got 1 star
params.putString("picture", "http://media-cerulean.cursecdn.com/attachments/8/944/1star.png"); //image copied from a Website
params.putString("description", "My score is Sufficient"); //summary of a driver
}
else if (driverStarScore == 2){ // if someone got 2 star
params.putString("picture", "http://media-cerulean.cursecdn.com/attachments/8/946/2star.png");//image copied from a Website
params.putString("description", "My score is Satisfactory");//summary of a driver
}
else if (driverStarScore == 3){ // if someone got 3 star
params.putString("picture", "http://media-cerulean.cursecdn.com/attachments/8/948/3star.png");
params.putString("description", "My score is Good");
}
else if (driverStarScore == 4){
params.putString("picture", "http://media-cerulean.cursecdn.com/attachments/8/950/4star.png");
params.putString("description", "My score is Really Good");
}
else if (driverStarScore == 5){
params.putString("picture", "http://media-cerulean.cursecdn.com/attachments/8/951/5star.png");
params.putString("description", "My score is Best");
}
fb.dialog(ActivityShare.this,"feed",params, new DialogListener(){
@Override
public void onFacebookError(FacebookError e){
//TODO Auto-generated method stub
}
@Override
public void onError(DialogError e){
//TODO Auto-generated method stub
}
@Override
public void onCancel(){
//TODO Auto-generated method stub
}
@Override
public void onComplete(Bundle values) {
// TODO Auto-generated method stub
Editor editor=sp.edit();
editor.putString("access_token",fb.getAccessToken());
editor.putLong("access_expires", fb.getAccessExpires());
editor.commit();
}
});
}
如果您使用的是 facebook sdk 版本 4
而不是使用此代码与文本共享图像。这对我有用。
1.declaire变量
private LoginManager manager;
2.put 在 onCreate 中更改布局名称
FacebookSdk.sdkInitialize(this.getApplicationContext());
setContentView(R.layout.Your_layout);
callbackManager = CallbackManager.Factory.create();
3.call此方法分享
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) {
shareImage();
}
@Override
public void onCancel() {
System.out.println("onCancel");
}
@Override
public void onError(FacebookException exception) {
System.out.println("onError");
}
});
}
private void shareImage() {
//use your image bitmap here
Bitmap image = BitmapFactory.decodeResource(getResources(),
R.drawable.ic_launcher);
SharePhoto photo = new SharePhoto.Builder().setBitmap(image)
.setCaption("Hey this is only demo text.!")
.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();
}
我正在制作一个 android 应用程序,对于这个应用程序,我需要将图片分享到 Facebook。我已经按照 facebook 共享网站上的所有程序进行操作。但是在单击共享按钮时它显示图像但在共享图像时丢失了。我的详细代码是-
public class ActivityShare extends Activity {
String[] ShareOption; //create a shareOption array
Spinner ShareOptionList; // define A spinner list to choose the score type
int driverStarScore = 4; // define a drivingstar score to give the star to a driver .
Facebook fb; // define facebook
SharedPreferences sp;
ImageView pic;
ListView list; // define list
String[] ShareItemName ={ //create a string array of shareItem name
"Dropbox",
"Email",
"Facebook",
"Google Plus",
"Twitter",
"Whatsapp",
};
Integer[] ShareImageId={ //create an integer array of images of the shareitem
R.drawable.ic_dropbox,
R.drawable.ic_email,
R.drawable.ic_facebook,
R.drawable.ic_googleplus,
R.drawable.ic_twitter,
R.drawable.ic_whatsapp,
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_share);
sp=getPreferences(MODE_PRIVATE);
String access_token= sp.getString("access_token", null);
long expires=sp.getLong("access_expires", 0);
if(access_token!=null){
fb.setAccessToken(access_token);
}
if(expires !=0){
fb.setAccessExpires(expires);
}
String APP_ID=getString(R.string.APP_ID);
fb=new Facebook(APP_ID);
ShareOptionList=(Spinner) findViewById(R.id.spinner_ShareScore);
ShareOption=getResources().getStringArray(R.array.ShareChooseOption);
ArrayAdapter<String> adapter1=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,ShareOption);
ShareOptionList.setAdapter(adapter1);
ShareOptionList.setOnItemSelectedListener(new OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
int index=arg0.getSelectedItemPosition();
Toast.makeText(getBaseContext(), "You select "+ ShareOption[index],Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
ShareListAdapter adapter=new ShareListAdapter(this, ShareItemName, ShareImageId);
list=(ListView)findViewById(R.id.listview_share);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
@SuppressWarnings("deprecation")
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
String Selecteditem= ShareItemName[+position]; //response to click on the list
Toast.makeText(ActivityShare.this, Selecteditem, Toast.LENGTH_SHORT).show();
if(Selecteditem=="Facebook"){ //if facebook item is pressed
Bundle params=new Bundle();
params.putString("name", "ASSES ME");
params.putString("caption", "My Driving Score");
params.putString("link", "http://www.mybringback.com/"); //the link of the app
if (driverStarScore == 1){ // if someone got 1 star
params.putString("picture", "http://media-cerulean.cursecdn.com/attachments/8/944/1star.png"); //image copied from a Website
params.putString("description", "My score is Sufficient"); //summary of a driver
}
else if (driverStarScore == 2){ // if someone got 2 star
params.putString("picture", "http://media-cerulean.cursecdn.com/attachments/8/946/2star.png");//image copied from a Website
params.putString("description", "My score is Satisfactory");//summary of a driver
}
else if (driverStarScore == 3){ // if someone got 3 star
params.putString("picture", "http://media-cerulean.cursecdn.com/attachments/8/948/3star.png");
params.putString("description", "My score is Good");
}
else if (driverStarScore == 4){
params.putString("picture", "http://media-cerulean.cursecdn.com/attachments/8/950/4star.png");
params.putString("description", "My score is Really Good");
}
else if (driverStarScore == 5){
params.putString("picture", "http://media-cerulean.cursecdn.com/attachments/8/951/5star.png");
params.putString("description", "My score is Best");
}
fb.dialog(ActivityShare.this,"feed",params, new DialogListener(){
@Override
public void onFacebookError(FacebookError e){
//TODO Auto-generated method stub
}
@Override
public void onError(DialogError e){
//TODO Auto-generated method stub
}
@Override
public void onCancel(){
//TODO Auto-generated method stub
}
@Override
public void onComplete(Bundle values) {
// TODO Auto-generated method stub
Editor editor=sp.edit();
editor.putString("access_token",fb.getAccessToken());
editor.putLong("access_expires", fb.getAccessExpires());
editor.commit();
}
});
}
如果您使用的是 facebook sdk 版本 4 而不是使用此代码与文本共享图像。这对我有用。
1.declaire变量
private LoginManager manager;
FacebookSdk.sdkInitialize(this.getApplicationContext());
setContentView(R.layout.Your_layout);
callbackManager = CallbackManager.Factory.create();
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) {
shareImage();
}
@Override
public void onCancel() {
System.out.println("onCancel");
}
@Override
public void onError(FacebookException exception) {
System.out.println("onError");
}
});
}
private void shareImage() {
//use your image bitmap here
Bitmap image = BitmapFactory.decodeResource(getResources(),
R.drawable.ic_launcher);
SharePhoto photo = new SharePhoto.Builder().setBitmap(image)
.setCaption("Hey this is only demo text.!")
.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();
}