2016-11-18 59 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

检查我编辑的答案。 –

相关问题