我从服务器获取数据并将其设置为listview。每个Listview项目都有照片(网址图片)和文字+分享按钮。我已经实现了所有代码并且工作得很完美..但是任何人都可以帮助我..如何在点击特定listview项目的按钮时实现facebook共享意图。我想分享图像和文本 我从某种方式问到这个
并提前感谢
答案 0 :(得分:0)
这是我的代码“MediaAdapter.java”:
public class MediaAdapter extends ArrayAdapter<Media> {
ArrayList<Media> mediaList;
Context context;
int Resource;
LayoutInflater vi;
ViewHolder holder;
ImageLoader imageLoader;
public MediaAdapter(Context context, int resource, ArrayList<Media> objects) {
super(context, resource, objects);
imageLoader = new ImageLoader(context);
vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Resource = resource;
mediaList = objects;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// convert view = design
View v = convertView;
v = vi.inflate(Resource, null);
holder = new ViewHolder();
int loader = R.drawable.ic_launcher;
//l url d image
holder.imageview = (ImageView) v.findViewById(R.id.urlImage);
// load image
imageLoader.DisplayImage(mediaList.get(position).getUrl(), loader, holder.imageview );
holder.titre = (TextView) v.findViewById(R.id.titre);
holder.titre.setText(mediaList.get(position).getTitre());
v.setTag(holder);
holder.button = (Button) v.findViewById(R.id.btnOne);
holder.button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Toast.makeText(v.getContext(), "test",Toast.LENGTH_SHORT).show();
}
});
return v;
}
static class ViewHolder {
public ImageView imageview;
public TextView titre;
public Button button;
}
}
答案 1 :(得分:0)
在代码中更改此内容....
首先从构造函数
中删除此行 imageLoader = new ImageLoader(context);
将其更改为
ImageLoader imageLoader = ImageLoader.getInstance();
其次从代码中删除此行:
imageLoader.DisplayImage(mediaList.get(position).getUrl(), loader, holder.imageview );
将其更改为
imageLoader.loadImage(mediaList.get(position).getUrl(), new SimpleImageLoadingListener() {
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
holder.button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT,mediaList.get(position).getTitre());
shareIntent.setType("image/*");
shareIntent.putExtra(Intent.EXTRA_STREAM, getImageUri(context,loadedImage));
try {
context.startActivity(shareIntent);
} catch (Exception ex) {
Toast.makeText(context, ex.getMessage(),Toast.LENGTH_LONG).show();
}
}
});
}
});
使用此方法将位图转换为uri:
public Uri getImageUri(Context inContext, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = Images.Media.insertImage(inContext.getContentResolver(),inImage, "Title", null);
return Uri.parse(path);
}
这将打开共享选项,以共享所有其他应用程序,包括Facebook。如果用户选择了Facebook,那么图像将在Facebook应用程序中打开(如果已安装)。让我知道它是否适合您。
答案 2 :(得分:0)
但我有一个问题:但是当我点击项目编号2的共享按钮时我遇到了问题=====项目编号3的内容是共享的并注意编号2,反之亦然? ??????????????
holder.button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Uri pictureUri = getLocalBitmapUri(holder.imageview);
if (pictureUri != null) {
// Construct a ShareIntent with link to image
String text = "image : "+mediaList.get(position).getTitre();
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, text);
shareIntent.putExtra(Intent.EXTRA_STREAM, pictureUri);
shareIntent.setType("image/*");
// Launch sharing dialog for image
shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(shareIntent);
} else {
// ...sharing failed, handle error
}
/////////////////////////////////////////////// ////////////
private Uri getLocalBitmapUri(ImageView imageview) {
// TODO Auto-generated method stub
// Extract Bitmap from ImageView drawable
Drawable drawable =holder.imageview.getDrawable();
Bitmap bmp = null;
if (drawable instanceof BitmapDrawable){
bmp = ((BitmapDrawable) holder.imageview.getDrawable()).getBitmap();
} else {
return null;
}
// Store image to default external storage directory
Uri bmpUri = null;
try {
File file = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOWNLOADS), "share_image_" + System.currentTimeMillis() + ".png");
file.getParentFile().mkdirs();
FileOutputStream out = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
out.close();
bmpUri = Uri.fromFile(file);
} catch (IOException e) {
e.printStackTrace();
}
return bmpUri;
}