13

My Application Want to share Image on Google Plus And I have read About Google Plus And API shows that we can post media File using PlusShare.Builder. And I have Also Searched In stackoverflow but ditn't Get any Proper Answer. And I have Tried with These Code is :

case R.id.Share:
         if (mPlusClient.isConnected()) {
         Intent shareIntent = new PlusShare.Builder(this,mPlusClient)
         .setType("image/*")
         .setText("Risk Score")
         .addStream(Uri.fromFile(new File(Path)))
         .getIntent();
         startActivityForResult(shareIntent, REQ_START_SHARE);
         }

        break;

And

  case R.id.Share:

Intent shareIntent = ShareCompat.IntentBuilder.from(PlusActivity.this)
   .setText("This site has lots of great information about Android! http://www.android.com")
.setType("image/*").addStream(Uri.fromFile(new File(Path)))
.getIntent().setPackage("com.google.android.apps.plus");             
 startActivityForResult(shareIntent, REQ_START_SHARE);
        break;

And

 case R.id.Share:

         Intent shareIntent = new PlusShare.Builder(this,mPlusClient)
         .setType("image/*")
         .setText("Risk Score")
         .addStream(Uri.fromFile(new File(Path)))
         .getIntent();
         startActivityForResult(shareIntent, REQ_START_SHARE);

        break;

And Every Time I am Getting Same Problem.

12-05 12:19:57.316: E/AndroidRuntime(4688): FATAL EXCEPTION: main
12-05 12:19:57.316: E/AndroidRuntime(4688): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.SEND typ=image/* flg=0x80000 pkg=com.google.android.apps.plus (has extras) }
12-05 12:19:57.316: E/AndroidRuntime(4688):     at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1518)
12-05 12:19:57.316: E/AndroidRuntime(4688):     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1390)
12-05 12:19:57.316: E/AndroidRuntime(4688):     at android.app.Activity.startActivityForResult(Activity.java:3204)
12-05 12:19:57.316: E/AndroidRuntime(4688):     at com.winit.android.riskfactor.PlusActivity.onClick(PlusActivity.java:133)
12-05 12:19:57.316: E/AndroidRuntime(4688):     at android.view.View.performClick(View.java:3517)
12-05 12:19:57.316: E/AndroidRuntime(4688):     at android.view.View$PerformClick.run(View.java:14155)
12-05 12:19:57.316: E/AndroidRuntime(4688):     at android.os.Handler.handleCallback(Handler.java:605)
12-05 12:19:57.316: E/AndroidRuntime(4688):     at android.os.Handler.dispatchMessage(Handler.java:92)
12-05 12:19:57.316: E/AndroidRuntime(4688):     at android.os.Looper.loop(Looper.java:154)
12-05 12:19:57.316: E/AndroidRuntime(4688):     at android.app.ActivityThread.main(ActivityThread.java:4624)
12-05 12:19:57.316: E/AndroidRuntime(4688):     at java.lang.reflect.Method.invokeNative(Native Method)
12-05 12:19:57.316: E/AndroidRuntime(4688):     at java.lang.reflect.Method.invoke(Method.java:511)
12-05 12:19:57.316: E/AndroidRuntime(4688):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
12-05 12:19:57.316: E/AndroidRuntime(4688):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
12-05 12:19:57.316: E/AndroidRuntime(4688):     at dalvik.system.NativeStart.main(Native Method)

And I have read these Question Also

sending-json-data-to-google-plus

android-google-plush-intregation-in-my-android-app-gives-android-content-activit

sending-json-data-to-google-plus

failing-to-share-on-google-with-android

no-activity-found-to-handle-intent-causes-fc

Community
  • 1
  • 1
Cropper
  • 1,177
  • 2
  • 14
  • 36

7 Answers7

6

Google+ does not have an independent write API available to general developers - the only way to post to Google+ via an app is to open the Google+ app (preferably using the Intent returned by PlusShare.Builder) So If Google plus is not present then it will give ActivityNotFoundException.

Ari
  • 1,296
  • 1
  • 18
  • 36
3

Try this link and follow the step by step procedure. I have followed the same and worked for me. Another thing is clean your project or restart eclipse IDE or Install Google+ app (apk) on your device or emulator.
You can also take a look at my answer posted here the step by step Procedure to install Google+ App on Emulator.
Hope it will solve your problem...

Edited:
Try this code on your btn_share

Intent shareIntent = ShareCompat.IntentBuilder.from(ExampleActivity.this)
          .setType("image/*")
          .setText("Welcome to the Google+ platform. https://developers.google.com/+")
          .getIntent()
          .setPackage("com.google.android.apps.plus");//this line is important you should set your package like in this code.

      startActivity(shareIntent);
Community
  • 1
  • 1
Zubair Ahmed
  • 2,857
  • 2
  • 27
  • 47
  • Thanks But My requirement is if there is no google+ than also My app can share Image file. – Cropper Dec 13 '13 at 05:33
  • I can understand what you want to do but the fact is, mostly when you want to share something to social media (like Facebook, twitter etc...) from your app you have to login first and then do sharing and for that purpose as far as I think you should have app installed in your device or Emulator so that you could perform login and do sharing what ever you want. – Zubair Ahmed Dec 13 '13 at 05:43
  • 2
    Please check this http://stackoverflow.com/questions/20471741/code-403-and-reason-forbidden-exception-at-uploading-file-in-google-p This code is trying to upload image file but getting exception can u help in this. – Cropper Dec 13 '13 at 05:54
  • Ok let me try and and I will comment. – Cropper Dec 13 '13 at 06:03
  • Goto this https://developers.google.com/+/mobile/android/getting-started link and take a look in detail about "Prerequisites" then you will got the idea what I am saying from start... – Zubair Ahmed Dec 13 '13 at 06:09
2

Google+ require Google+ installed on the device. Added this code to catch exception.

try {
    startActivityForResult(builder.getIntent(), 0);
} catch (ActivityNotFoundException e) {
    Toast.makeText(YourActivity.this, "You haven't installed google+ on your device", Toast.LENGTH_SHORT).show();
}
Arst
  • 3,098
  • 1
  • 35
  • 42
1

Yes you can share images on Google plus with PlusBuilder. Add the Google Play Services library as a reference in your project. This Method calls com.google.android.gms.plus.PlusShare$Builder class for sharing. If Google plus is not present then it will give ActivityNotFoundException.

MainActivity.java

 import android.app.Activity;
 import android.content.ContentResolver;
 import android.content.Intent;
 import android.content.IntentSender.SendIntentException;
 import android.net.Uri;
 import android.os.Bundle;
 import android.view.Menu;
 import android.view.View;
 import android.view.View.OnClickListener;
 import android.widget.Button;

 import com.google.android.gms.common.ConnectionResult;
 import com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks;
 import com.google.android.gms.common.GooglePlayServicesClient.OnConnectionFailedListener;
 import com.google.android.gms.plus.PlusClient;
 import com.google.android.gms.plus.PlusOneButton;
 import com.google.android.gms.plus.PlusShare;

 public class MainActivity extends Activity implements ConnectionCallbacks, OnConnectionFailedListener{




private static final int PLUS_ONE_REQUEST_CODE = 0;
private static final int REQ_SELECT_PHOTO = 1;
private static final int REQ_START_SHARE = 2;
private PlusClient mPlusClient;
Button share;
private static final int REQUEST_CODE_RESOLVE_ERR = 9000;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mPlusClient = new PlusClient.Builder(MainActivity.this, MainActivity.this, MainActivity.this)
    .build();


    share=(Button)findViewById(R.id.share);
    share.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent photoPicker = new Intent(Intent.ACTION_PICK);
            photoPicker.setType("image/*");
            startActivityForResult(photoPicker, REQ_SELECT_PHOTO);
        }
    });
}



@Override
public void onConnectionFailed(ConnectionResult result) {
    // TODO Auto-generated method stub
    if (result.hasResolution()) {
        try {
            result.startResolutionForResult(this, REQUEST_CODE_RESOLVE_ERR);
        } catch (SendIntentException e) {
            mPlusClient.connect();
        }

    }
}


@Override
public void onConnected(Bundle arg0) {
    // TODO Auto-generated method stub

}


@Override
public void onDisconnected() {
    // TODO Auto-generated method stub

}

@Override
protected void onActivityResult(int requestCode, int responseCode, Intent intent) {
    if (requestCode == REQUEST_CODE_RESOLVE_ERR && responseCode == RESULT_OK) {
        mPlusClient.connect();
        return;
    }
    if(requestCode == REQ_SELECT_PHOTO) {
        if(responseCode == RESULT_OK) {
            Uri selectedImage = intent.getData();
            ContentResolver cr = this.getContentResolver();
            String mime = cr.getType(selectedImage);

            PlusShare.Builder share = new PlusShare.Builder(this);
            share.setText("hello everyone!");
            share.addStream(selectedImage);
            share.setType(mime);
            startActivityForResult(share.getIntent(), REQ_START_SHARE);
        }
    }
}

@Override
protected void onResume() {
    super.onResume();        
    //  mPlusOneStandardButtonWithAnnotation.initialize( URL, PLUS_ONE_REQUEST_CODE);
}

@Override
protected void onStart() {
    super.onStart();
    mPlusClient.connect();
}
@Override
protected void onStop() {
    super.onStop();
    mPlusClient.disconnect();

}
 }

share is a button and galley will be opened on its click.

Add following lines of code in AndroidManifest.xml under application tag

   <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

and the Internet permission:

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

This is a working example of Google Plus Sharing.

Abhishek Agarwal
  • 1,907
  • 12
  • 21
1

Just perform a check if google services are available

        int errorCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
        if (errorCode != ConnectionResult.SUCCESS) {
            GooglePlayServicesUtil.getErrorDialog(errorCode, this, 0).show();
        } else {
            // Do somethings
        }

You need to import this package:

import com.google.android.gms.common.GooglePlayServicesUtil;

NOTE: Usually the emulator don't have Google Play Services installed except if you has selected Google APIs instead of Android APIs for your emulated device but in this case you must check also if google account is available.

JDCoder
  • 541
  • 6
  • 9
  • Even you get SUCCESSyou will still get ActivityNotFound Exception if Google+ is not installed. – Arst Oct 21 '14 at 01:17
1

This is related somehow to .settype(). I had .setType("text/html") and was experiencing this error. But after I have changed it to .setType("text/plain") the problem was solved. I don't know why, but the .settype() is the reason of this problem.

Andranik
  • 2,729
  • 1
  • 29
  • 45
1

for me I forgot to add setType("image/*") and now it works fine

Ma7moud El-Naggar
  • 548
  • 1
  • 6
  • 18