2016-11-18 58 views
0

我必須將圖片與文字共享到所有社交媒體。所以我嘗試了下面的代碼: -Android共享意圖在某些應用程序中不起作用

share.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 

       Uri uri = imageUrl; 

       Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND); 
       shareIntent.setType("text/html"); 
       shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, (String) 
         v.getTag(R.string.app_name)); 
       shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, 
         "Text for post"); 
       shareIntent.putExtra(Intent.EXTRA_STREAM, uri); 
       context.startActivity(Intent.createChooser(shareIntent, "Share Using")); 

      } 
     }); 

它正在工作,現在我可以在應用程序中與gmail中的圖像共享文本。但問題是,即使我安裝並更新了所有這些應用程序,我仍無法通過使用此共享意圖獲取Facebook,Twitter和Instagram。

我需要讓所有的社交媒體應用程序共享。

被Facebook出現,但不能共享圖像使用「文本/純」作爲shareIntent型...

有人可以幫助我找到答案嗎?

在此先感謝。

+0

做你嘗試\ */\ * –

+0

對不起,不工作,只股份的文本。我必須分享文字和image.and還當我選擇了Facebook的分享它顯示空白文本:( – max

+0

我已經得到像「http://example.com/image.jpg」字符串的圖像。所以我必須將其轉換爲Uri然後只有我必須通過圖像與文本參數共享社交媒體 – max

回答

0

試試這個工作對我來說,

void share(String nameApp, Uri imagePath) { 


    boolean isAppExist = false; 
    try { 
     List<Intent> targetedShareIntents = new ArrayList<Intent>(); 
     Intent share = new Intent(Intent.ACTION_SEND); 
     share.setType("image/*"); 
     List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(share, 0); 
     if (!resInfo.isEmpty()) { 
      for (ResolveInfo info : resInfo) { 
       Intent targetedShare = new Intent(Intent.ACTION_SEND); 
       targetedShare.setType("image/*"); // put here your mime type 
       if (info.activityInfo.packageName.toLowerCase().contains(nameApp) || info.activityInfo.name.toLowerCase().contains(nameApp)) { 
        targetedShare.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.share_title)); 
        targetedShare.putExtra(Intent.EXTRA_TEXT, shareMessage); 
        if (imagePath != null) 
         targetedShare.putExtra(Intent.EXTRA_STREAM, imagePath); 
        targetedShare.setPackage(info.activityInfo.packageName); 
        targetedShareIntents.add(targetedShare); 
        isAppExist = true; 
       } 
      } 
      Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(0), "Select app to share"); 
      chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[]{})); 
      startActivityForResult(chooserIntent, REQUEST_SHARE); 
     } 
    } catch (Exception e) { 
     Utils.setLog("Exception while sending image on" + nameApp + " " + e.getMessage()); 
    } 

    if (!isAppExist) { 
     Dialogs.showAlert(this, null, getString(R.string.share_no_application_found), true, false); 
    } 


} 

nameApp你必須通過中,你必須張貼圖像應用程序的名稱。在你的情況下通過facebook

+0

顯示空白帖子 – max

0

試試下面的代碼:

public void share(final String url, final String text) { 
    new Thread(new Runnable() { 
     @Override 
     public void run() { 
      try { 
       Intent share = new Intent(Intent.ACTION_SEND); 
       share.setType("image/jpeg"); 
       share.putExtra(
         Intent.EXTRA_TEXT, 
         "Sharing from " 
           + context.getString(R.string.app_name) 
           + "\n" + text); 

       if (url != null) { 
        Bitmap bmp = null; 
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 

        try { 
         byte[] chunk = new byte[4096]; 
         int bytesRead; 
         InputStream stream = new URL(url).openStream(); 

         while ((bytesRead = stream.read(chunk)) > 0) { 
          outputStream.write(chunk, 0, bytesRead); 
         } 

         outputStream.toByteArray(); 

         bmp = BitmapFactory.decodeByteArray(
           outputStream.toByteArray(), 0, 
           outputStream.toByteArray().length); 
        } catch (IOException e) { 
         e.printStackTrace(); 
         Log.v("Error", e.toString()); 
        } 

        String filename = Environment.getExternalStorageDirectory().getAbsoluteFile() + File.separator + File.separator 
          + Utils.getCurrentTimeInFormate() + ".png"; 
        Log.e("BITMAP", filename); 
        FileOutputStream out = new FileOutputStream(filename); 
        bmp.compress(Bitmap.CompressFormat.PNG, 50, out); 

        Bitmap icon = bmp; 

        ContentValues values = new ContentValues(); 
        values.put(MediaStore.Images.Media.TITLE, "title"); 
        values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg"); 
        Uri uri = context.getContentResolver().insert(
          MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); 

        OutputStream outstream; 
        try { 
         outstream = context.getContentResolver() 
           .openOutputStream(uri); 
         icon.compress(Bitmap.CompressFormat.PNG, 60, outstream); 
         outstream.close(); 
        } catch (Exception e) { 
         System.err.println(e.toString()); 
        } 
        share.putExtra(Intent.EXTRA_STREAM, uri); 
       } 
       context.startActivity(Intent.createChooser(share, "Share")); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }).start(); 
} 
+0

在res = new GetResponse(null);在這種情況下什麼是res – max

+0

忽略該行 –

+0

檢查我編輯的答案。 –

相關問題