putExtra()video uri

时间:2015-10-29 14:43:26

标签: android android-intent video uri

我尝试将一个视频文件从我的galery放到另一个活动并播放它。 我有两个问题。

  1. 当我想选择视频时,在我有可能选择该文件之前,我会转到第二个活动。

  2. 我找不到使用PutExtra()传递文件并在第二项活动中获取文件的方法。

  3. 这是第一项活动:

       package com.roa.saveandplayvideo;
    
    import android.content.Intent;
    import android.net.Uri;
    import android.os.Bundle;
    import android.os.Environment;
    import android.provider.MediaStore;
    import android.support.design.widget.FloatingActionButton;
    import android.support.design.widget.Snackbar;
    import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.Toolbar;
    import android.util.Log;
    import android.view.View;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.widget.Button;
    
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    import java.io.File;
    import java.util.Locale;
    
    public class MainActivity extends AppCompatActivity {
    
        public static final String TAG = MainActivity.class.getSimpleName();
        public static final int TAKE_PHOTO_REQUEST = 0;
        public static final int TAKE_VIDEO_REQUEST = 1;
        public static final int PICK_PHOTO_REQUEST = 2;
        public static final int PICK_VIDEO_REQUEST = 3;
    
        public static final int MEDIA_TYPE_IMAGE = 4;
        public static final int MEDIA_TYPE_VIDEO = 5;
    
        public static final int FILE_SIZE_LIMIT = 1024*1024*10; // 10MBs
    
        protected Uri mMediaUri;
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
    
            Button btn1 = (Button)findViewById(R.id.button1);
            Button btn2 = (Button)findViewById(R.id.button2);
            Button btn3 = (Button)findViewById(R.id.button3);
    
            //take a video...
            btn1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
                    mMediaUri = getFileUri(MEDIA_TYPE_VIDEO);
                    takeVideoIntent.putExtra(MediaStore.EXTRA_OUTPUT, mMediaUri);
                    takeVideoIntent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, 10);
                    takeVideoIntent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0);
                    startActivityForResult(takeVideoIntent,TAKE_VIDEO_REQUEST);
    
                }
            });
    //choose the video
            btn2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent chooseVideo = new Intent(Intent.ACTION_GET_CONTENT);
                    chooseVideo.setType("video/*");
                    startActivityForResult(chooseVideo, PICK_VIDEO_REQUEST);
    
                    Intent playVid = new Intent(MainActivity.this, PlayActivity.class);
                    playVid.putExtra("vidFile",chooseVideo);
                    startActivity(playVid);
    
                }
            });
    
    
    
        }
    
        private Uri getFileUri(int mediaTypeVideo) {
            //get the Uri
            //1. get the external storage directory
            String appName = MainActivity.this.getString(R.string.appName);
            File theFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), appName);
    
    //2.create our subdirectory
            if(!theFile.exists()){
                if(!theFile.mkdir()){
                    Log.e(TAG, "Failed to make the directory");
                    return  null;
                }
            }
            //3.create the file name
            //4.create the file
            File mediaFile;
            Date now = new Date();
            String timeStamp = new SimpleDateFormat("yyyMMdd_HHmmss", Locale.getDefault()).format(now);
            String path = theFile.getPath() + File.separator;
            mediaFile = new File(path + "VID" + timeStamp + ".mp4");
    
            return Uri.fromFile(mediaFile);
        }
    
    }
    

    这是第二项活动:

    package com.roa.saveandplayvideo;
    
    import android.content.Intent;
    import android.net.Uri;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.widget.VideoView;
    
    public class PlayActivity extends AppCompatActivity {
    
        private Uri file;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main2);
            Intent i = getIntent();
            i.getExtra("vidFile", file);
    
    
            VideoView vid = (VideoView)findViewById(R.id.videoView1);
            vid.setVideoURI(file);
            vid.start();
    
    
        }
    }
    

    感谢。

1 个答案:

答案 0 :(得分:1)

btn2设置监听器时,您正在开始两项活动。删除第二个意图。

//choose the video
btn2.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent chooseVideo = new Intent(Intent.ACTION_GET_CONTENT);
        chooseVideo.setType("video/*");
        startActivityForResult(chooseVideo, PICK_VIDEO_REQUEST);
    }
});

您需要覆盖onActivityResult

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(requestCode==PICK_VIDEO_REQUEST){
        if(resultCode==RESULT_OK){
            Uri vidFile = data.getData();
            Intent playVid = new Intent(MainActivity.this, PlayActivity.class);
            playVid.putExtra("vidFile",vidFile);
            startActivity(playVid);
        }
    }

}

然后在PlayACtivity中获取Uri并播放视频:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    Intent i = getIntent();
    file = Uri.parse(i.getExtras().get("vidFile").toString());
    VideoView vid = (VideoView)findViewById(R.id.videoView1);
    vid.setVideoURI(file);
    vid.start();
}