2012-10-15 68 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-card的照片地址。有什麼問題我不理解? –

+0

@DeepikaLalra我有類似的問題。你能建議嗎? – Apparatus

回答

2

整合ForGooglePlus活動在你的代碼,並把URL(IMAGEURL),描述(description文本)和的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

我也對谷歌以及通過使用Android上的意圖我正在拍攝設備的屏幕截圖張貼圖片和張貼在谷歌加,我用你的代碼我得到異常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; 
     } 
    } 

謝謝你,至於 尼廷

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是actully目前

otherway是擁有谷歌加SDK是小的jar文件與您可以共享圖像

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

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); 
     } 
    } 
} 
相關問題