Image share (post) on facebook from resourse drawable folder

I am making an application for which I need to share an image with text on facebook timeline. However with my code i can share a link to the facebook wall but cannot share Image. I already tried most of the code of stack overflow. but not succeed yet. Here is my code.

MainActivity.java

 package com.example.test2;



import com.facebook.android.Facebook;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;

public class MainActivity extends Activity {

String[] ShareOption;
Spinner ShareOptionList;
int driverStarScore = 1; //2 or 3 ...
Facebook facebookClient;    
SharedPreferences mPrefs;

ListView list;

 String[] ShareItemName ={

 "Dropbox",
 "Email",
 "Facebook",
 "Google Plus",
 "Twitter",
 "Whatsapp",

 };

 Integer[] ShareImageId={

         R.drawable.ic_dropbox,
         R.drawable.ic_email,
         R.drawable.ic_facebook,
         R.drawable.ic_googleplus,
         R.drawable.ic_twitter,
         R.drawable.ic_whatsapp,

         };
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);






    ShareOptionList=(Spinner) findViewById(R.id.spinner_ShareScore);
    ShareOption=getResources().getStringArray(R.array.ShareChooseOption);
    ArrayAdapter<String> adapter1=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,ShareOption);
    ShareOptionList.setAdapter(adapter1);

    ShareOptionList.setOnItemSelectedListener(new OnItemSelectedListener(){

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
            // TODO Auto-generated method stub
            int index=arg0.getSelectedItemPosition();
            Toast.makeText(getBaseContext(), "You select "+ ShareOption[index],Toast.LENGTH_LONG).show();
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            // TODO Auto-generated method stub

        }



    });




    ShareListAdapter adapter=new ShareListAdapter(this, ShareItemName, ShareImageId);
    list=(ListView)findViewById(R.id.listview_share);
    list.setAdapter(adapter);

    list.setOnItemClickListener(new OnItemClickListener() {

        @Override
         public void onItemClick(AdapterView<?> parent, View view,
         int position, long id) {
         // TODO Auto-generated method stub
         String Selecteditem= ShareItemName[+position];

         Toast.makeText(MainActivity.this, Selecteditem, Toast.LENGTH_SHORT).show();

         if(Selecteditem=="Facebook"){  


            // if (driverStarScore == 1){
                 //Uri pngUri = Uri.parse("file//res/drawable/star_1.png");
            // }
             //if (driverStarScore == 2){
                // Uri pngUri = Uri.parse("file//res/drawable/star_2.png");
        // }

            String urlToShare = "http://tinypic.com/r/16gil4y/8";

             try {

                    Intent facebook1 = new Intent();
                    facebook1.setClassName("com.facebook.katana", "com.facebook.katana.activity.composer.ImplicitShareIntentHandler");
                    facebook1.setAction("android.intent.action.SEND");
                    facebook1.setType("image/png");
                    facebook1.putExtra("android.intent.extra.TEXT", urlToShare);
                    startActivity(facebook1);

             } catch (Exception e) {
                    // If we failed (not native FB app installed), try share through SEND
                    Intent facebook = new Intent(Intent.ACTION_SEND);
                    String sharerUrl = "https://www.facebook.com/sharer/sharer.php?u=" + urlToShare;
                    facebook = new Intent(Intent.ACTION_VIEW, Uri.parse(sharerUrl));
                    startActivity(facebook);
                }


         }
         if(Selecteditem=="Email"){
            Intent email = new Intent(Intent.ACTION_SEND);

            email.setType("message/rfc822");
            email.putExtra(Intent.EXTRA_EMAIL  , new String[]{"recipient@example.com"}); // if you want to add email address also.
            email.putExtra(android.content.Intent.EXTRA_TEXT, "Sample Text");
            email.putExtra(Intent.EXTRA_SUBJECT, "Driving Score Email");

            try {
                startActivity(Intent.createChooser(email, "Send mail..."));
            } catch (android.content.ActivityNotFoundException ex) {
                Toast.makeText(MainActivity.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
            }
         }

        }
         });



}

/**
*
*/

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }





@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.


    return super.onOptionsItemSelected(item);
}

}

AndroidManifest.xml

        <?xml version="1.0" encoding="utf-8"?>
        <manifest xmlns:android="http://schemas.android.com/apk/res/android"
            package="com.example.test2"
            android:versionCode="1"
            android:versionName="1.0" >

            <uses-sdk
                android:minSdkVersion="16"
                android:targetSdkVersion="18" />

            <application
                android:allowBackup="true"
                android:icon="@drawable/ic_launcher"
                android:label="@string/app_name"
                android:theme="@style/AppTheme" >
                <activity
                    android:name=".MainActivity"
                    android:label="@string/app_name" >
                    <intent-filter>
                        <action android:name="android.intent.action.MAIN" />

                        <category android:name="android.intent.category.LAUNCHER" />
                    </intent-filter>
                </activity>
                <activity
                    android:name=".Menu"
                    android:label="@string/app_name" >
                    <intent-filter>
                        <action android:name="android.intent.action.Menu" />
                        <data android:mimeType="image/png" />
                        <category android:name="android.intent.category.DEFAULT" />
                    </intent-filter>
                </activity>

              <activity
                        android:name="com.sample.socialshare.facebookUpload"
                        android:label="@string/app_name"
                        android:theme="@android:style/Theme.DeviceDefault.Dialog">
                        <intent-filter>
                            <action android:name="android.intent.action.VIEW" />
                            <category android:name="android.intent.category.DEFAULT" />
                        </intent-filter>
                </activity>


                        </application>
                <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>


        </manifest>


Source: facebook

Leave a Reply