2017-08-25 39 views
0

我想通过shareIntent分享图片。我下面的代码给出:无法在Gmail和Instagram中共享图片

Intent shareIntent = new Intent(Intent.ACTION_SEND); 
shareIntent.setType("image/*"); 
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(list.get(position).getFilePath())); 
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Sending from myApp");//gmail-subject 
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Image 1234");//gmail-body 
shareIntent.putExtra(Intent.EXTRA_TITLE, "Image 1234"); 
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 
startActivity(Intent.createChooser(shareIntent, "Share via")); 

我能够发送图像WhatsApp的Facebook的但不是在的GmailInstagram的

当试图通过的Gmail分享它显示我

Can't attach file

Instagram的它显示我

无法加载图像

是否需要添加任何其他内容shareIntent

回答

0
File file = new File(Environment.getExternalStorageDirectory() + File.separator + yourFile.getRouteFile()); 
         if (file.exists()) { 
          String fileExtension = FileExtension.getFileExtension(file); 
          Log.d(TAG, "fileExtension: " + fileExtension); 
          Uri uri = Uri.parse("file://" + file.getAbsolutePath()); 
          Intent share = new Intent(Intent.ACTION_SEND); 
          share.putExtra(Intent.EXTRA_STREAM, uri); 
          share.setType(fileExtension + "/*"); 
          share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 
          context.startActivity(Intent.createChooser(share, "Share file")); 
         } 

GET文件扩展名

public static String getFileExtension(File file) { 
     String name = file.getName(); 
     try { 
      return name.substring(name.lastIndexOf(".") + 1); 
     } catch (Exception e) { 
      return ""; 
     } 
    } 
+0

这工作得很好。但有1个问题:share.setType(fileExtension +“/ *”);由于这一行,它向我展示了与share.setType(“image/*”)相比较的可共享应用程序的小列表。 – Kesha

+0

只能使用图像 –

+0

也是答案的正确答案 –