如果在Facebook上分享图像,我如何传递指定的图像网址?我在viewpager上有3张图片。所以每个图像都可以使用分享按钮
在Facebook上分享继承我的主要活动代码:
public class MainActivity extends Activity {
Button btnShare;
ViewPager viewPager;
PagerAdapter adapter;
int[] bookCover = new int[] {
R.drawable.image1,
R.drawable.image2,
R.drawable.image3,
};
//for image1 - the url is http;//image1.com
//for image2 - the url is http;//image2.com
//for image3 - the url is http;//image3.com
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupFacebookConnect(savedInstanceState);
if( Build.VERSION.SDK_INT >= 9){
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
btnShare = (Button) findViewById(R.id.btnShare);
btnShare.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
shareImage(string url);
}
});
// Locate the ViewPager in viewpager_main.xml
viewPager = (ViewPager) findViewById(R.id.pager);
// Pass results to ViewPagerAdapter Class
adapter = new ViewPagerAdapter(this, bookCover);
// Binds the Adapter to the ViewPager
viewPager.setAdapter(adapter);
viewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@SuppressLint("NewApi")
@Override
public void onPageSelected(int position) {
// When changing pages, reset the action bar actions since they are dependent
// on which page is currently active. An alternative approach is to have each
// fragment expose actions itself (rather than the activity exposing actions),
// but for simplicity, the activity provides the actions in this sample.
invalidateOptionsMenu();
}
});
public void shareImage(string url) {
Bundle params = new Bundle();
params.putString("name", "Facebook SDK for Android");
params.putString("caption", "Build great social apps and get more installs.");
params.putString("description", "The Facebook SDK for Android makes it easier and faster to develop Facebook integrated Android apps.");
params.putString("link", "https://developers.facebook.com/android");
params.putString("picture", imageFromURL);
WebDialog feedDialog = (
new WebDialog.FeedDialogBuilder(MainActivity.this,
Session.getActiveSession(),
params))
.setOnCompleteListener(new OnCompleteListener() {
@Override
public void onComplete(Bundle values,
FacebookException error) {
if (error == null) {
// When the story is posted, echo the success
// and the post Id.
final String postId = values.getString("post_id");
if (postId != null) {
Toast.makeText(MainActivity.this,
"Posted story, id: "+postId,
Toast.LENGTH_SHORT).show();
} else {
// User clicked the Cancel button
Toast.makeText(MainActivity.this,
"Publish cancelled",
Toast.LENGTH_SHORT).show();
}
} else if (error instanceof FacebookOperationCanceledException) {
// User clicked the "x" button
Toast.makeText(MainActivity.this,
"Publish cancelled",
Toast.LENGTH_SHORT).show();
} else {
// Generic, ex: network error
Toast.makeText(MainActivity.this,
"Error posting story",
Toast.LENGTH_SHORT).show();
}
}
})
.build();
feedDialog.show();
}
并继承我的页面适配器:
public class ViewPagerAdapter extends PagerAdapter {
Context context;
int[] bookCover;
LayoutInflater inflater;
Button share;
private ProgressDialog progressDialog;
public ViewPagerAdapter(Context context, int[] bookCover) {
this.context = context;
this.bookCover = bookCover;
}
@Override
public int getCount() {
return bookCover.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((RelativeLayout) object);
}
@Override
public Object instantiateItem(ViewGroup container, final int position) {
final PdfHandler pdf = new PdfHandler(context);
final int pdfPos = position;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.viewpager_item, container, false);
ImageView imgflag = (ImageView) itemView.findViewById(R.id.flag);
// Capture position and set to the ImageView
//imgflag.setScaleType(ScaleType.FIT_XY);
imgflag.setImageResource(bookCover[position]);
// Add viewpager_item.xml to ViefeewPager
((ViewPager) container).addView(itemView);
imgflag.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
v.setEnabled(false);
// TODO Auto-generated method stub
if(pdfPos == 0) {
pdf.openPdf("pdf");
}
if(pdfPos == 1) {
pdf.openPdf("pdf");
}
if(pdfPos == 2) {
pdf.openPdf("pdf");
}
}
});
return itemView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// Remove viewpager_item.xml from ViewPager
((ViewPager) container).removeView((RelativeLayout) object);
}