0

I am saving some images in a folder of internal memory and displaying these all saved images on a button click . now i want to share current open image on social site as facebook, gmail etc.I m able to share text but not image.

Code for saving image is...

 RelativeLayout content = (RelativeLayout) findViewById(R.id.relative);
        content.setDrawingCacheEnabled(true);
        Bitmap bitmap = content.getDrawingCache();

        File myDir=new File("/sdcard/MyCollection");
        myDir.mkdirs();
        Random generator = new Random();
        int n = 10000;
        n = generator.nextInt(n);
        String fname = "Image-"+ n +".jpg";
        File file = new File (myDir, fname);
        if (file.exists ()) file.delete ();
        FileOutputStream outStream;
        try {
            outStream = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
            outStream.flush();
            outStream.close();
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }

code for image access is .......

ImageButton sharingButton = new ImageButton(this); sharingButton.setLayoutParams(new ViewGroup.LayoutParams(RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT)); sharingButton.setImageResource(R.drawable.alert);

    getFromfolder();
    String[] projection = {MediaStore.Images.Thumbnails._ID};

    cursor = managedQuery( MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
            projection,
            null,
            null,
            MediaStore.Images.Thumbnails.IMAGE_ID);
    columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
    GridView sdcardImages = (GridView) findViewById(R.id.gridview);
    sdcardImages.setAdapter(new ImageAdapter());
    sdcardImages.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView parent, View v, int position, long id) {
            String[] projection = {MediaStore.Images.Media.DATA};
            cursor = managedQuery( MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                    projection,
                    null,
                    null,
                    null);
            columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToPosition(position);
            String imagePath = cursor.getString(columnIndex);
        }
    });

}

public void getFromfolder()
{
    File file= new File(android.os.Environment.getExternalStorageDirectory(),"MyCollection");

    if (file.isDirectory())
    {
        listFile = file.listFiles();
        for (int i = 0; i < listFile.length; i++)
        {
            f.add(listFile[i].getAbsolutePath());
        }
    }
}

public class ImageAdapter extends BaseAdapter {
    private LayoutInflater mInflater;

    public ImageAdapter() {
        mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    public int getCount() {
        return f.size();
    }
    public Object getItem(int position) {
        return position;
    }
    public long getItemId(int position) {
        return position;
    }
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(
                    R.layout.gelleryitem, null);
            holder.imageview = (ImageView) convertView.findViewById(R.id.thumbImage);
            convertView.setTag(holder);
        }
        else {
            holder = (ViewHolder) convertView.getTag();
        }
        Bitmap myBitmap = BitmapFactory.decodeFile(f.get(position));
        holder.imageview.setImageBitmap(myBitmap);
        return convertView;
    }
}
class ViewHolder {
    ImageView imageview;
}
Soni Kumar
  • 283
  • 1
  • 4
  • 16

2 Answers2

0

To add the image as an email attachment look here, at the Intent.EXTRA_STREAM section: https://developer.android.com/guide/components/intents-common.html#Email

For Facebook look here: https://developers.facebook.com/docs/sharing/android

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
geokavel
  • 619
  • 4
  • 12
0

First you have to fetch the image as a bitmap from the file path. Then you can pass the image to other applications through an intent. If you want a string to passed along with the image, you can pass it through the intent as EXTRA_TEXT. Add WRITE_EXTERNAL_STORAGE in the permissions. Try this:

File imgFile = new  File(imagePath);
if(imgFile.exists()){
      Bitmap icon = BitmapFactory.decodeFile(imagePath);
      Intent share = new Intent(Intent.ACTION_SEND);
      share.setType("image/jpeg");

      ContentValues values = new ContentValues();
      values.put(Images.Media.TITLE, "title");
      values.put(Images.Media.MIME_TYPE, "image/jpeg");
      Uri uri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI,
        values);


      OutputStream outstream;
      try {
         outstream = getContentResolver().openOutputStream(uri);
         icon.compress(Bitmap.CompressFormat.JPEG, 100, outstream);
      outstream.close();
      } catch (Exception e) {
         System.err.println(e.toString());
      }

      //text
      share.putExtra(Intent.EXTRA_TEXT, "Text that has to be shared");
      //image
      share.putExtra(Intent.EXTRA_STREAM, uri);
      startActivity(Intent.createChooser(share, "Share Image"));
}
Jossy Paul
  • 1,267
  • 14
  • 26
  • If you want to put a caption with your image on Facebook you have to use this. http://stackoverflow.com/questions/22533773/android-how-to-share-image-with-text-on-facebook-via-intent/25065367#25065367 – geokavel Oct 05 '15 at 16:01