activity_video_app_main.xml
<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".VideoAppMainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Record"
android:id="@+id/recordButton"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Play"
android:id="@+id/playButton"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:singleLine="true" />
<VideoView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/videoView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_above="@+id/recordButton" />
VideoAppMainActivity
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.VideoView;
public class VideoAppMainActivity extends Activity {
private Button mRecordView, mPlayView;
private VideoView mVideoView;
private int ACTIVITY_START_CAMERA_APP = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_app_main);
mRecordView = (Button) findViewById(R.id.recordButton);
mPlayView = (Button) findViewById(R.id.playButton);
mVideoView = (VideoView) findViewById(R.id.videoView);
mRecordView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent callVideoAppIntent = new Intent();
callVideoAppIntent.setAction(MediaStore.ACTION_VIDEO_CAPTURE);
startActivityForResult(callVideoAppIntent, ACTIVITY_START_CAMERA_APP);
}
});
mPlayView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mVideoView.start();
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == ACTIVITY_START_CAMERA_APP && resultCode == RESULT_OK) {
Uri videoUri = data.getData();
mVideoView.setVideoURI(videoUri);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_video_app_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
menu_video_app_main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" tools:context=".VideoAppMainActivity">
<item android:id="@+id/action_settings" android:title="@string/action_settings"
android:orderInCategory="100" android:showAsAction="never" />
</menu>
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="nigelhenshaw.com.videoapp" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".VideoAppMainActivity"
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>
</manifest>
的strings.xml
<resources>
<string name="app_name">VideoApp</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
</resources>
答案 0 :(得分:-1)
从6.0 Marshmallow你必须得到许可 android中有两种类型的权限 查看此链接
Go here and read and try to get permission dynamically when user open camera 1st time
或者您可以尝试此代码
private void requestPermission()
{
checkPermission();
if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE)){
Toast.makeText(getApplicationContext(),"Storage permission allows us to send Image. Please allow in App Settings for Complete your order.",Toast.LENGTH_LONG).show();
} else {
ActivityCompat.requestPermissions(activity,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},PERMISSION_REQUEST_CODE);
}
}
private boolean checkPermission(){
int result = ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (result == PackageManager.PERMISSION_GRANTED){
return true;
} else {
return false;
}
}
点击
添加// click of Button ImageCamera
case R.id.ImageCamera:
if(!checkPermission())
requestPermission();
break;
还要在Manifest
中添加它<!--external storage -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />