首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何通过android应用程序在google Plus中分享图片?

如何通过android应用程序在google Plus中分享图片?
EN

Stack Overflow用户
提问于 2012-10-15 15:54:16
回答 6查看 10K关注 0票数 18

我已经尝试过这个代码,但我没有看到我的帐户中分享的照片。

代码语言:javascript
复制
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);
EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2015-12-03 20:14:53

将ForGooglePlus活动集成到您的代码中,并为其添加URL(imageUrl)、description(描述文本)和contentUrl(URL)。注意: bellow代码在我的应用程序中也有效。

代码语言:javascript
复制
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();
            }
        }
    }

}
票数 4
EN

Stack Overflow用户

发布于 2012-10-18 05:48:35

Google+应用程序仅支持content:// URIs。为此,您需要使用MediaStore应用编程接口。

代码语言:javascript
复制
 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");
票数 26
EN

Stack Overflow用户

发布于 2013-01-22 23:37:10

我还通过安卓在谷歌+上张贴图片,使用意图我正在拍摄设备的屏幕截图并将其发布在谷歌+上,我使用了你的代码我得到了异常FileNotFoundException(),正如你提到的使用绝对路径我得到了错误,方法getAbsolutePath()是未定义的类型字符串我的代码在下面给出请建议我在代码中更正

代码语言:javascript
复制
    package com.testproject;


    import java.io.File;
    import java.io.FileNotFoundException;

    import android.app.Activity;
    import android.content.Intent;
    import android.graphics.Bitmap;
    import android.net.Uri;
    import android.os.Bundle;
    import android.provider.MediaStore;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.RelativeLayout;

    public class TestProjectActivity extends Activity {

        private Button share_btn = null; 
        private String url=null;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            share_btn = (Button)findViewById(R.id.share_btn);
            share_btn.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    Intent intent=new Intent(TestProjectActivity.this,ShareDialogActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);    
                    startActivityForResult(intent, 1);  
                }
            });
        }
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            String url =takeScreenShot();
            super.onActivityResult(requestCode, resultCode, data);
            switch (resultCode) {
            case 1:
                String share = data.getExtras().getString("NAME");
                if(share!=null && share.equalsIgnoreCase("Share with Instagram")){
                    Intent i = new Intent(Intent.ACTION_SEND);
                    i.setType("image/jpg");
                    i.putExtra(Intent.EXTRA_STREAM, Uri.parse(url));
                    startActivity(Intent.createChooser(i, "Share Image"));
                }

                if(share!=null && share.equalsIgnoreCase("Share with GooglePlus")){
                    Intent shareIntent = new Intent(Intent.ACTION_SEND);
                    File tmpFile = new File(url);
                    String photoUri=null;
                    photoUri = url.getAbsolutePath();
                    try {
                        photoUri = MediaStore.Images.Media.insertImage(
                                getContentResolver(), tmpFile.getAbsolutePath(), null, null);
                        shareIntent = ShareCompat.IntentBuilder.from(this)
                        .setText("Hello from Google+!")
                        .setType("image/jpeg")
                        .setStream(Uri.parse(photoUri))
                        .getIntent()
                        .setPackage("com.google.android.apps.plus");
                        startActivity(shareIntent);
                    } catch (FileNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                break;
            }
        }
        public String takeScreenShot(){
            try{
                RelativeLayout  view = (RelativeLayout)findViewById(R.id.icflag_layout);
                View v1 = view.getRootView();
                System.out.println("Root View : "+v1);
                v1.setDrawingCacheEnabled(true);
                Bitmap bm = v1.getDrawingCache();
                url =MediaStore.Images.Media.insertImage(getContentResolver(), bm,"screeshot.jpg", 1233+ ".jpg Card Image");
            }
            catch(OutOfMemoryError e){

            }
            return url;
        }
    }

谢谢并向Nitin致以问候

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12891382

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档