Sharing Image and Text On twitter not working

2k views Asked by At

This is my code given below.FrameLayout is Used to share as JPEG Image on twitter.This is the code Im trying .

sharesp=(Spinner)findViewById(R.id.spnrShare);
        sharesp.setAdapter(new MyShAdapter(MainActivity.this, R.layout.rowview, Sharestring));
        sharesp.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View v,
                    int position, long arg3) {
                // TODO Auto-generated method stub
                selectitem=parent.getItemAtPosition(position).toString();
                selecetint=position;

                if(selectitem=="FaceBook" || selecetint==R.drawable.fbbtn){
                       shareFbook();

                }
                else if(selectitem=="Twitter" || selecetint==R.drawable.twitbtn){
                    shareTwitter();

                }
            }

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

            }
        });


public void shareTwitter(){

         FrameLayout savFrame_layout=(FrameLayout)findViewById(R.id.frame);
        try {
            if(MainActivity.this.mainFrame_layout==null){
            MainActivity.this.mainFrame_layout.setDrawingCacheEnabled(true);
              MainActivity.this.mainFrame_layout.refreshDrawableState();
              MainActivity.this.mainFrame_layout.buildDrawingCache();

            MainActivity.this.bm_ImgFrame = mainFrame_layout.getDrawingCache();

        int i1=100000;
        Random random=new Random();
        i1=random.nextInt(i1);
           MainActivity.fname = "Quick_"+ i1 + ".jpg";

            String pathy=Environment.getExternalStorageDirectory()+File.separator+"/MYAnApps";
            MainActivity.this.rootFile=new File(pathy);

            MainActivity.this.sdImageMainDirectory = new File(MainActivity.this.rootFile + MainActivity.fname);
            FileOutputStream fileOutputStream = new FileOutputStream(MainActivity.this.sdImageMainDirectory);
            bm_ImgFrame.compress(CompressFormat.JPEG, 80, fileOutputStream);

            Uri uri = Uri.parse(MainActivity.this.sdImageMainDirectory.getAbsolutePath());

                Intent intent = new Intent(Intent.ACTION_SEND);
                intent.setType("/*");
                intent.setClassName("com.twitter.android", "com.twitter.android.PostActivity");
                intent.putExtra(Intent.EXTRA_TEXT, "MyAndroidApp: https://play.google.com/store/apps/details?id=com.example.MyApp");
                intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(MainActivity.this.sdImageMainDirectory));
                startActivity(intent);            
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   

    }
2

There are 2 answers

0
GrIsHu On

Try using "*/*" as MIME, so you can send any generic data type.

 intent.setType("*/*");

Also try changing ACTION_SEND to ACTION_SEND_MULTIPLE which specialized for delivering multiple data.

More info about ACTION_SEND_MULTPLE and handling MIME types.

1
NagarjunaReddy On

Check this Example this is working for me

Twitter Image And text sharing Example

twitter sharing using this one

  private void showTwittDialog() {

    final Dialog dialog = new Dialog(getApplicationContext());
    dialog.setContentView(R.layout.twitt_dialog);
    dialog.setTitle("Twitter");

    Button btnPost = (Button) dialog.findViewById(R.id.btnpost);
    final EditText et = (EditText) dialog.findViewById(R.id.twittext);
    et.setText(sharingStr);
    et.setSelection(et.getText().length());
    btnPost.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
              twitt_msg = et.getText().toString().trim();   
            if (twitt_msg.length() == 0) {
                showToast("Tweet is empty!!!");
                return;
            } else if (twitt_msg.length() > 140) {
                showToast("Tweet of more than 140 characters not allowed!!!");
                return;
            }
            dialog.dismiss();
            new PostTwittTask().execute(twitt_msg);
        }
    });
    dialog.show();
}

protected void Share_to_twitter() {  
    Intent tweetIntent = new Intent();
    tweetIntent.setType("text/plain");
    tweetIntent.putExtra(Intent.EXTRA_TEXT, sharingStr);
    final PackageManager packageManager =  getPackageManager();
    List<ResolveInfo> list = packageManager.queryIntentActivities(tweetIntent, PackageManager.MATCH_DEFAULT_ONLY);

    for (ResolveInfo resolveInfo : list) {
        twitter_dialog = resolveInfo.activityInfo.packageName;
    }

    if (twitter_dialog != null  && twitter_dialog.startsWith("com.twitter.android")) {
        // if the native twitter app is found
        tweetIntent.setPackage(twitter_dialog);
        startActivity(tweetIntent);
    } else {
        showTwittDialog();
    }
    mTwitter.resetAccessToken();        
}

class PostTwittTask extends AsyncTask<String, Void, String> {
    ProgressDialog pDialog;

    @Override
    protected void onPreExecute() {
        pDialog = new ProgressDialog(getApplicationContext());
        pDialog.setMessage("Posting Twitt...");
        pDialog.setCancelable(false);
        pDialog.show();
        super.onPreExecute();
    }

    @Override
    protected String doInBackground(String... twitt) {
        try {
            mTwitter.updateStatus(twitt[0]);
            return "success";

        } catch (Exception e) {
            if (e.getMessage().toString().contains("duplicate")) {
                return "Posting Failed because of Duplicate message...";
            }
            e.printStackTrace();
            return "Posting Failed!!!";
        }
    }

    @Override
    protected void onPostExecute(String result) {
        pDialog.dismiss();

        if (null != result && result.equals("success")) {               
            View popUpView = getLayoutInflater().inflate(R.layout.share_successfully_popup,null);   
            final PopupWindow popUp = new PopupWindow(popUpView,UtilityClass.dynamicScalingForWidth(100),
                    UtilityClass.dynamicScalingForWidth(100),true);
            popUp.showAtLocation(popUpView.findViewById(R.id.share_successfully_ctr),Gravity.CENTER,0, 0); 

            Handler handler = new Handler();
            Runnable r = new Runnable() {
                public void run() {
                    popUp.dismiss();
                }
            };
            handler.postDelayed(r, 3000);           

        } else {
            showToast(result);
        }
        super.onPostExecute(result);
    }
}