2012-10-15 7 views
17

이 코드를 이미 시도했지만 계정에서 사진을 공유하지 못했습니다.Android 앱을 통해 Google Plus에서 이미지를 공유하는 방법은 무엇입니까?

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); 
+0

로그를 확인 했습니까? – pedrofurla

+0

응용 프로그램이 시작될 때 "사진을 SD 카드에만 선물 할 수 있습니다"라는 메시지가있는 축배를 보여줍니다. 하지만 난 sd 카드에서 사진의 주소를주고있다. 내가 이해하지 못하는 문제는 무엇인가? –

+0

@DeepikaLalra 비슷한 문제가 있습니다. 제발 제안 할 수 있니? – Apparatus

답변

2

코드에 ForGooglePlus 활동을 통합하고 URL (이미지 URL), 설명 (설명 텍스트)와 동일한에 대한 contentUrl (URL)를 넣습니다. 참고 : 벨로우즈 코드 또한 내 앱에서 작동했습니다.

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

} 
26

Google+ 앱은 content:// URIs 만 지원합니다. 이 목적으로 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"); 
+0

그것은 나에게 FileNotFoundException()을 준다. 이미지의 전체 경로를 얻지 못하기 때문에 "sdcard/1346249742258.jpg"만 찍습니다. –

+0

절대 경로를 사용해보십시오. –

+1

이렇게하려면 장치에 Google+가 사용 목적으로 정확하게 설치되어 있어야합니다. 의도를 사용하고 싶지 않습니다. 프로그래밍 방식으로 처리 할 수 ​​있습니까? 여기에 내 질문입니다 .http : //stackoverflow.com/questions/18955084/share-photo-on-google-plus-using-android-sdk-not-by-share-intent – Scorpion

3

또한 구글에 플러스 내가 장치의 스크린 샷을 복용하고 안드로이드 사용 의도을 통해 이미지를 게시하고 구글에 게시 플러스, i)는 코드 내가 예외 FileNotFoundException이을 (무엇입니까를 사용하면 사용하는 언급으로하고 절대 경로 내가니틴

오류가 메소드 getAbsolutePath()가

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

감사합니다 코드 나에게 수정을 제안 해주십시오 내 코드 아래에 주어진 String 형에 대한 정의되지했고 에 관하여

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

이것은 저에게 효과적입니다. 구글 플러스 앱이 장치에 방금 장치에서 네이티브 응용 프로그램을 시작하는 것입니다 코딩 한 방법이 없기 때문에

1

헤이 디 피카 당신이 파일도 발견 예외를 얻고있다

그것은 단지 기본을 경우 작동합니다 AP는

otherway 그와 함께 작은 jar 파일을 구글 플러스 SDK는 당신이 당신의 API의 아래 사용하여 이미지를 공유 할 수 있습니다

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

0

이미지를 공유 할 수있는 것입니다 actully 존재합니다. 자세한 단계는 튜토리얼을 확인

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); 
     } 
    } 
} 
관련 문제