18

このコードは既に試しましたが、アカウントで写真が共有されていません。

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);
4

6 に答える 6

26

Google+ アプリはcontent:// URIのみをサポートしています。MediaStoreこの目的のために APIを使用する必要があります。

 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");
于 2012-10-17T21:48:34.780 に答える
3

私はまた、デバイスのスクリーンショットを撮ってGoogle Plusに投稿しているインテントを使用して、Androidを介してGoogle Plusに画像を投稿しています.コードを使用しました.FileNotFoundException()という例外が発生しています。メソッド getAbsolutePath() は String 型に対して定義されていません。私のコードを以下に示します。コードの修正を提案してください。

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

ありがとう、よろしくニティン

于 2013-01-22T15:37:10.407 に答える
1

HEy Deepika、あなたはファイルを取得しておらず、例外も見つかりませんでした

ネイティブ ap が実際に存在する場合にのみ機能します。

そうでなければ、画像を共有できる小さなjarファイルであるgoogle plus sdkを用意することです

https://developers.google.com/+/mobile/android/

于 2013-03-21T10:27:06.200 に答える
1

絶対パスは使用しないでください。

OnActivityResult() は、カメラから画像をキャプチャした後にこれを使用します。

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

これは私のために働いています。

于 2013-03-05T08:52:23.440 に答える
0

以下のAPIを使用して画像を共有できます。詳細な手順については、チュートリアルを確認してください

http://androidsrc.net/integrating-google-plus-sign-in-into-your-android-application/

 /**
     * API to process media post request start activity with MIME type as video
     * and image
     */
    private void processShareMedia() {
        Intent photoPicker = new Intent(Intent.ACTION_PICK);
        photoPicker.setType("video/*, image/*");
        startActivityForResult(photoPicker, PICK_MEDIA_REQUEST_CODE);

    }

/**
 * Handle results for your startActivityForResult() calls. Use requestCode
 * to differentiate.
 */
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == PICK_MEDIA_REQUEST_CODE) {
        // If picking media is success, create share post using
        // PlusShare.Builder
        if (resultCode == RESULT_OK) {
            Uri selectedImage = data.getData();
            ContentResolver cr = this.getContentResolver();
            String mime = cr.getType(selectedImage);

            PlusShare.Builder share = new PlusShare.Builder(this);
            share.setText("Hello from AndroidSRC.net");
            share.addStream(selectedImage);
            share.setType(mime);
            startActivityForResult(share.getIntent(),
                    SHARE_MEDIA_REQUEST_CODE);
        }
    }
}
于 2015-02-08T08:58:19.953 に答える