0

I am fetching data from server and set it into listview. Each Listview item have photo ( url image ) and a text + share button . I have implemented all the code and working perfect.. But can any one help me.. How to implement facebook share intent when click on button of particular listview item.I want to share image and text i ask from a way to the this

and thank you in advance

hinata
  • 23
  • 6
  • Check out this link -http://stackoverflow.com/questions/22533773/android-how-to-share-image-with-text-on-facebook-via-intent .Basically to share both image and text ,you have to implement the facebook SDK .Without sdk only image or text ,but not both can be shared. – mik dass Apr 20 '16 at 17:53
  • thank you for your answer but the main problem is : i have the image and the title stored in a database ( am very very new in android this is my first ferst app ) – hinata Apr 20 '16 at 21:19
  • The database thing is fine but to share your image and text both to Facebook app, you need the Facebook SDK. If you don't want to import the Facebook SDK, then you can only share images, not text. – mik dass Apr 21 '16 at 18:53
  • Paste the code and I will tell you how to implement the share button in the getView() method to share the photo(bitmap) without the sdk.Do note that you cannot share the text without the SDK implemented,only bitmaps and urls can be shared without the SDK . – mik dass Apr 21 '16 at 19:33
  • ok thank you so much – hinata Apr 22 '16 at 19:17

3 Answers3

0

this is my code "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;


    }

}

hinata
  • 23
  • 6
0

Change this in the code ....

First remove this line from the constructor

  imageLoader = new ImageLoader(context); 

Change it to

   ImageLoader imageLoader = ImageLoader.getInstance();

Secondly remove this line from the code :

imageLoader.DisplayImage(mediaList.get(position).getUrl(), loader, holder.imageview );

Change it to

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();
              }
         }         
    });
   }
});

Use this method to transform bitmap to 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);
}

This will open the the share option for sharing to all the other apps including facebook. If facebook is selected by the user,then the image will be opened in the facebook app (if installed).Let me know if it working for you or not.

mik dass
  • 381
  • 3
  • 16
  • firstly thank you so much for the help :) but i have some error in the code 1- imageUri cannot be resolved to a variable 2- SimpleImageLoadingListener cannot be resolved to a type 3 - and i have chaged the type of " Bitmap loadedImage " to " final Bitmap loadedImage " – hinata Apr 23 '16 at 14:58
  • @bouchra Edited my answer ,check it out.Also about the SimpleImageLoadingListener error, import it to the adapter by using import statement.Let me know if it's working or not. – mik dass Apr 23 '16 at 19:12
  • @bouchra Let me know if the solution worked for you. – mik dass Apr 24 '16 at 18:22
  • the problem was solved and now i can share image stored in a server ( Mysql DATABASE ) of each item using "intent " i can share it with the applications early installed in the smart phone but now i would like to use Facebook SDK ( if the user doesn't have facebook app)??? – hinata Apr 25 '16 at 20:49
  • @bouchra Good to know it worked for you. To use the Facebook SDK, you will need a Facebook ID and a developer account. Check out the Facebook developer website for more information .I haven't use the SDK yet but will try soon. BTW if my answer helped you, mark it as the best answer. – mik dass Apr 26 '16 at 20:03
0

this is my new coode it work like a charm

but i have a problem : but i have a problem when i click on share button of the item Number 2 ===== the content of the item number 3 is shared and note the Number 2 and vice-versa ????????????????

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;
}     
hinata
  • 23
  • 6