How to share text & image in Google Plus (G+) from

2019-01-23 10:06发布

问题:

Guys i want to share in G+. As i knew there are G+ API same as FaceBook & Twitter. I get this doc and follow the same process.

I have found that we can share via two different like ,

  • Deep linking
  • Interactive posts

and base on that i have to choose DeepLink for sharing.

i have reach up to here but when i try to copy and paste that code in my new_project then its not working. says like

The constructor PlusShare.Builder(Activity) is not visible.

I found a lot but at the end i get same API link. Don't know how to achieve this task. I have done sharing in FaceBook & Twiiter but not get success in G+.

Please Guys help me.

Thanks in advance

回答1:

public void share_image_text_GPLUS() {
    File pictureFile;

    try {
        File rootSdDirectory = Environment.getExternalStorageDirectory();

        pictureFile = new File(rootSdDirectory, "attachment.jpg");
        if (pictureFile.exists()) {
            pictureFile.delete();
        }
        pictureFile.createNewFile();

        FileOutputStream fos = new FileOutputStream(pictureFile);

        URL url = new URL("http://img.youtube.com/vi/AxeOPU6n1_M/0.jpg");
        HttpURLConnection connection = (HttpURLConnection) url
                .openConnection();
        connection.setRequestMethod("GET");
        connection.setDoOutput(true);
        connection.connect();
        InputStream in = connection.getInputStream();

        byte[] buffer = new byte[1024];
        int size = 0;
        while ((size = in.read(buffer)) > 0) {
            fos.write(buffer, 0, size);
        }
        fos.close();

    } catch (Exception e) {

        System.out.print(e);
        // e.printStackTrace();
        return;
    }

    Uri pictureUri = Uri.fromFile(pictureFile);

    Intent shareIntent = ShareCompat.IntentBuilder.from(this)
            .setText("Hello from Google+!").setType("image/jpeg")
            .setStream(pictureUri).getIntent()
            .setPackage("com.google.android.apps.plus");
    startActivity(shareIntent);
}

    buttonLoadImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            share_image_text_GPLUS();

        }
    });