Android calls system camera to record and save

Source: Internet
Author: User

1, add the following code in the Androidmanifest.xml
<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "Http://schemas.android.com/apk/res/android"
<uses-permission android:name= "Android.permission.INTERNET"/>
<uses-permission android:name= "Android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name= "Android.permission.MOUNT_UNMOUNT_FILESYSTEMS"
Tools:ignore= "Protectedpermissions"/>
<application
<provider
Android:authorities= "${applicationid}.provider"
Android:name= "Android.support.v4.content.FileProvider"
Android:exported= "false"
Android:granturipermissions= "true" >
<meta-data android:name= "Android.support.FILE_PROVIDER_PATHS"
Android:resource= "@xml/provider_paths" >
</meta-data>
</provider>
</application></manifest>
2. Create a new XML folder in the Res directory and create a new provider_paths XML file
<?xml version= "1.0" encoding= "Utf-8"? ><paths xmlns:android= "Http://schemas.android.com/apk/res/android" >    <external-path name= "External_files" path= "." /></paths>
3. Open the camera in the main file
Intent intent=new Intent (mediastore.action_video_capture);
try {

Fileuri=uri.fromfile (Createmediafile ()); If this is the case, it will be an error
Fileuri= Fileprovider.geturiforfile (Cameravideoactivity.this,getapplicationcontext (). GetPackageName () + ". Provider ", Createmediafile ());//This is the correct wording

} catch (IOException e) {
E.printstacktrace ();
}
Intent.putextra (Mediastore.extra_output,fileuri);
Intent.putextra (mediastore.extra_video_quality,1);

Startactivityforresult (intent,1);
4. How to create and save videos
Private File Createmediafile () throws IOException {
if (utils.checksdcardavaliable ()) {
if ((Environment.getexternalstoragestate (). Equals (environment.media_mounted))) {
Select your Own folder
String path = Environment.getexternalstoragedirectory (). GetPath () + "/myvideo/";
Constants.video_url is a constant that represents the folder where the video is stored
File Mediastoragedir = new file (path);
if (!mediastoragedir.exists ()) {
if (!mediastoragedir.mkdirs ()) {
LOG.E ("TAG", "Folder creation failed");
return null;
}
}

The file is named according to the current number of milliseconds
String TimeStamp = string.valueof (System.currenttimemillis ());
TimeStamp = timestamp.substring (7);
String imagefilename = "V" + timeStamp;
String suffix = ". mp4";
File MediaFile = new file (Mediastoragedir + file.separator + imagefilename + suffix);
return mediafile;
}
}
return null;
}
}
5. Overriding the Onactivityresult method
@Override
protected void Onactivityresult (int requestcode, int resultcode, Intent data) {
if (requestcode==1)
{
if (RESULTCODE==RESULT_OK)
{
Toast.maketext (This, "Video saved to:\n" +
Data.getdata (), Toast.length_long). Show ();
Vv_play.setvideouri (Fileuri);
Vv_play.requestfocus ();
}
}
}
6, DDMS

Android calls system camera to record and save

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.