add image to twitter share intent android

Hugo Boss picture Hugo Boss · Oct 1, 2013 · Viewed 15.3k times · Source

I'm trying to add an image to my twitter share intent. I save an image locally in one class and then in another I get the image and try to attach to my intent.

Here is my code

private void shareTwitter(){

    try {

        FileInputStream fis;
        fis = getActivity().openFileInput("photo.jpg");
        Bitmap shot = BitmapFactory.decodeStream(fis);
        File file = new File(MapView.path, "snapshot.jpg");
        if(file.exists()){
            Log.i("FILE", "YES");
        }else{
            Log.i("FILE", "NO");
        }
        Uri uri = Uri.parse(file.getAbsolutePath());
        //Uri uri = Uri.parse("android.resource://com.gobaby.app/drawable/back");             
            Intent intent = new Intent(Intent.ACTION_SEND);
            intent.setType("/*");
            intent.setClassName("com.twitter.android", "com.twitter.android.PostActivity");
            intent.putExtra(Intent.EXTRA_TEXT, "Thiws is a share message");
            intent.putExtra(Intent.EXTRA_STREAM, uri);
            startActivity(intent);            

    } catch (final ActivityNotFoundException e) {
        Toast.makeText(getActivity(), "You don't seem to have twitter installed on this device", Toast.LENGTH_SHORT).show();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }   
}

At the moment there is no exception in my logcat my app just displays a toast saying image failed to load.

Please what an I doing wrong?

Answer

Amanni picture Amanni · Oct 3, 2013

This is what you need

intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file);