2016-12-01 105 views
1

我有一个视频文件路径,并希望在社交媒体上共享视频,但无法共享视频。我正在尝试遵循Android Studio 2.2中的代码,但它不起作用。Android:共享意图不适用于视频文件路径

代码片段:

public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Button shareBtn = (Button) findViewById(R.id.sharebutton); 

    shareBtn .setOnClickListener(
      new Button.OnClickListener() { 
       public void onClick(View v) { 

        File f = new File("/sdcard/VID_20161201123613.mp4"); 
        Uri uriPath = Uri.parse(f.getPath()); 

        Intent shareIntent = new Intent(); 
        shareIntent.setAction(Intent.ACTION_SEND); 
        shareIntent.putExtra(Intent.EXTRA_TEXT, "Text");     
        shareIntent.putExtra(Intent.EXTRA_STREAM, uriPath); 
        shareIntent.setType("video/*"); 
        shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 
        startActivity(Intent.createChooser(shareIntent, "send")); 

       } 
      } 
    ); 
} 
+0

你寄电子邮件想要显示列表以共享 –

回答

1

试试这个:

public void shareVideo(final String title, String path) { 

MediaScannerConnection.scanFile(getActivity(), new String[] { path }, 

      null, new MediaScannerConnection.OnScanCompletedListener() { 
       public void onScanCompleted(String path, Uri uri) { 
        Intent shareIntent = new Intent(
          android.content.Intent.ACTION_SEND); 
        shareIntent.setType("video/*"); 
        shareIntent.putExtra(
          android.content.Intent.EXTRA_SUBJECT, title); 
        shareIntent.putExtra(
          android.content.Intent.EXTRA_TITLE, title); 
        shareIntent.putExtra(Intent.EXTRA_STREAM, uri); 
        shareIntent 
          .addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); 
        context.startActivity(Intent.createChooser(shareIntent, 
          getString(R.string.str_share_this_video))); 

       } 
      }); 
} 
+1

在WhatsApp上共享时出现错误 - 文件格式不受支持,并且对于Gmail - 无法附加emp ty文件。 – SRK

+0

你想分享哪种类型的视频? –

+0

http://www.androidcode.ninja/android-share-intent-example/检查这个链接那就是你想要的 –

1

使用此代码为挑选从SD卡中的视频,然后用视频....

Intent sendIntent = new Intent(Intent.ACTION_SEND); 
    sendIntent.setType("video/3gp"); 
    sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Video"); 
    sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://sdcard/dcim/Camera/filename.3gp")); 
    sendIntent.putExtra(Intent.EXTRA_TEXT, "Enjoy the Video"); 
    startActivity(Intent.createChooser(sendIntent, "Email:"));