简体   繁体   中英

How to make a feature to share content in social media?

I made an application with a feature to share news with social media where in there is news content and news images. I've tried to follow some tutorials but still can not do it successfully.

So far the news content is sent without image. This my code:

Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("*/*");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, tittle_selected);
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, catagory_selected);
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, date_selected);
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, news_selected);
sharingIntent.putExtra(Intent.EXTRA_STREAM, image_selected);
startActivity(Intent.createChooser(sharingIntent,"Share using"));

Do it like this :

   ImageView image = (ImageView) findViewById(R.id.yourImage);
   final Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();

    try{
        new AsyncTask<String, String, String>(){
            @Override
            protected String doInBackground(String... params){
                String url = null;
                try{
                    url= MediaStore.Images.Media.insertImage(context.getContentResolver(), bitmap, o.getHeading(), o.getDescription());
                } catch (Exception e){
                    e.printStackTrace();
                }
                 return url;
            }

            @Override
            public void onPostExecute(String url){
                if(url != null){
                    Intent sharingIntent = new Intent();
                    sharingIntent.setAction(Intent.ACTION_SEND);
                    sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, tittle_selected);
                    sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, catagory_selected);
                    sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, date_selected);
                    sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, news_selected);
                    sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(url));
                    sharingIntent.setType("image/*");
                    sharingIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                    startActivity(Intent.createChooser(sharingIntent, "Share"));
                } else {
                    Toast.makeText(context, "Oops, a problem occurred while sharing. Check permission for this app.", Toast.LENGTH_LONG).show();
                }
            }
        }.execute();
    } catch (Exception e){
        e.printStackTrace();
    }

This is how intent is used in sharing data to any media

   Intent sendIntent = new Intent();
                    sendIntent.setAction(Intent.ACTION_SEND);
                    sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
                    sendIntent.setType("text/plain");
                    startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to)));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM