Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to share image in google Plus through an android app?

I have already tried this code, but i didn't saw photo shared in my account.

File file = new File("sdcard/1346249742258.jpg");
String photoUri = null;
photoUri = file.getAbsolutePath();

Intent shareIntent = ShareCompat.IntentBuilder.from(this)
        .setText("Sharing an image on Google!").setType("image/jpeg")
        .setStream(Uri.parse(photoUri)).getIntent()
        .setPackage("com.google.android.apps.plus");
startActivity(shareIntent);
like image 259
Deepika Lalra Avatar asked Oct 15 '12 07:10

Deepika Lalra


People also ask

Can I share Google Photos with more than one person?

You can share directly to anyone with a Google Account if they're in your contacts or by search using their email address or phone number. For everyone else, you can create a link to share. When you share a link to your album, anyone with the link can view and add to the album or photos.

What is partner sharing in Google Photos?

Automatically Save Photos to Your Phone Partner Sharing allows you to have any photo including a specific face that's shared with you to be saved to your library.


2 Answers

The Google+ app only supports content:// URIs. You will need to use the MediaStore API for this purpose.

 File tmpFile = new File("/path/to/image");
 final String photoUri = MediaStore.Images.Media.insertImage(
         getContentResolver(), tmpFile.getAbsolutePath(), null, null);

 Intent shareIntent = ShareCompat.IntentBuilder.from(this)
         .setText("Hello from Google+!")
         .setType("image/jpeg")
         .setStream(Uri.parse(photoUri))
         .getIntent()
         .setPackage("com.google.android.apps.plus");
like image 191
Chirag Shah Avatar answered Oct 12 '22 13:10

Chirag Shah


Integrate ForGooglePlus Activity in your code and put URL(imageUrl) ,Description(description text) and contentUrl(URL) for the same. Note : bellow code also worked in my app.

public class ForGooglePlus extends Activity
{
    private String imageUrl, description, contentUrl;
    private Context mContext;
    private int REQUEST_FOR_GOOGLE_PLUS = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        mContext = this;
        imageUrl = getIntent().getStringExtra("URL");
        description = getIntent().getStringExtra("Description");
        contentUrl = getIntent().getStringExtra("contentUrl");

        if (isPackageInstalled("com.google.android.apps.plus", mContext)) {
            if (imageUrl == null) {
                imageUrl = "";
            }
            if (description == null) {
                description = "";
            }
            // Intent shareIntent = new PlusShare.Builder(this)
            // .setType("image/jpeg")
            // .setText(description)
            // .setStream(getUriFromUrl(imageUrl))
            // .setContentUrl(Uri.parse(contentUrl))
            // .getIntent();

            Uri uri = getUriFromUrl(imageUrl);
            if (uri != null) {
                Intent shareIntent = ShareCompat.IntentBuilder
                        .from(ForGooglePlus.this)
                        .setText(description + "\n" + contentUrl)
                        .setType("image/jpeg").setStream(uri).getIntent()
                        .setPackage("com.google.android.apps.plus");
                startActivityForResult(shareIntent, REQUEST_FOR_GOOGLE_PLUS);
            } else {
                Intent shareIntent = ShareCompat.IntentBuilder
                        .from(ForGooglePlus.this)
                        .setText(description + "\n" + contentUrl)
                        .setType("image/jpeg").getIntent()
                        .setPackage("com.google.android.apps.plus");
                startActivityForResult(shareIntent, REQUEST_FOR_GOOGLE_PLUS);
            }
        } else {
            Toast.makeText(mContext, "Application not found", Toast.LENGTH_LONG)
                    .show();
            finish();
        }
    }

    public Uri getUriFromUrl(String thisUrl) {
        try {

            Bitmap inImage = ImageLoader.getInstance().loadImageSync(thisUrl);
            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
            String path = Images.Media.insertImage(
                    mContext.getContentResolver(), inImage, "Title", null);
            return Uri.parse(path);
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();

        }
        return null;
    }

    private boolean isPackageInstalled(String packagename, Context context) {
        PackageManager pm = context.getPackageManager();
        try {
            pm.getPackageInfo(packagename, PackageManager.GET_ACTIVITIES);
            return true;
        } catch (NameNotFoundException e) {
            return false;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == REQUEST_FOR_GOOGLE_PLUS) {
            if (resultCode == RESULT_OK) {
                finish();
            } else {
                Toast.makeText(mContext,
                        mContext.getString(R.string.msg_gp_cancel),
                        Toast.LENGTH_LONG).show();
                finish();
            }
        }
    }

}
like image 30
nilesh patel Avatar answered Oct 12 '22 12:10

nilesh patel