如何制作在社交媒体上分享内容的功能?

时间:2017-02-08 05:03:59

标签: java android

我创建了一个应用程序,用于与社交媒体分享新闻 哪里有新闻内容和新闻图片。我曾尝试过一些教程,但仍然无法成功。

到目前为止,新闻内容没有图像发送。这是我的代码:

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"));

2 个答案:

答案 0 :(得分:0)

这样做:

   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();
    }

答案 1 :(得分:0)

这就是将意图用于向任何媒体共享数据的方式

   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)));