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

前端 未结 6 1751
既然无缘
既然无缘 2020-12-24 07:54

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


        
6条回答
  •  醉话见心
    2020-12-24 08:51

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

提交回复
热议问题