Android 视频录制限制时间

  • Post author:
  • Post category:其他


package com.example.movetest;

import java.io.File;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.media.ThumbnailUtils;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.provider.MediaStore.Video;
import android.provider.MediaStore.Video.VideoColumns;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity implements OnClickListener {
	private static final int VIDEO_CAPTURE = 0;
	private Button btns;
	private ImageView imageView;
	String filePath="";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		btns = (Button) findViewById(R.id.moves);
		btns.setOnClickListener(this);
		imageView = (ImageView) findViewById(R.id.imageviews);
		imageView.setOnClickListener(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.moves:

			Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
			intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
			intent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, 10);//限制录制时间10秒
			startActivityForResult(intent, VIDEO_CAPTURE);
			break;

		case R.id.imageviews:
			
				Intent intents = new Intent(Intent.ACTION_VIEW);
				intents.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				intents.setDataAndType(Uri.fromFile(new File(filePath)), "video/*");
				startActivity(intents);
			
			break;
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		if (resultCode == Activity.RESULT_OK && requestCode == VIDEO_CAPTURE) {
			Uri uri = data.getData();
			Cursor cursor = this.getContentResolver().query(uri, null, null,
					null, null);
			if (cursor != null && cursor.moveToNext()) {
				int id = cursor.getInt(cursor.getColumnIndex(VideoColumns._ID));
				 filePath = cursor.getString(cursor
						.getColumnIndex(VideoColumns.DATA));
//				Bitmap bitmap = Thumbnails.getThumbnail(getContentResolver(),
//						id, Thumbnails.MICRO_KIND,null);
				imageView.setImageBitmap(getBitmap(filePath));
				cursor.close();
			}
		}

		super.onActivityResult(requestCode, resultCode, data);

	}
	/**
	 * 获得视频的缩略图
	 * 
	 * @param mUri
	 */
	public Bitmap getBitmap(String imgPath) {

		if (getAndroidSDKVersion() >= 8) {
			Bitmap bp = ThumbnailUtils.createVideoThumbnail(imgPath,
					Video.Thumbnails.MINI_KIND);
			return bp;
		} else {
			return null;
		}
	}
	/**
	 * 当前系统的版本号
	 * 
	 * @author liningning
	 * 
	 * @create 2011-11-14
	 */
	public int getAndroidSDKVersion() {
		int version;
		try {
			version = Integer.valueOf(android.os.Build.VERSION.SDK);
		} catch (NumberFormatException e) {
			System.out.println(e.toString());
			return -1;
		}
		return version;
	}

}

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <Button 
        android:id="@+id/moves"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="录制视频"
        />
<ImageView 
    android:src="@drawable/ic_launcher"
    android:id="@+id/imageviews"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />
</LinearLayout>

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.movetest"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.movetest.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
<uses-permission 
    android:name="android.permission.CAMERA"	
    />
</manifest>



版权声明:本文为guliang28原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。