7 package com.google.appinventor.components.runtime;
18 import android.app.Activity;
19 import android.content.ContentValues;
20 import android.content.Intent;
21 import android.net.Uri;
22 import android.os.Environment;
23 import android.provider.MediaStore;
24 import android.util.Log;
25 import android.Manifest;
28 import java.util.Date;
39 @DesignerComponent(version = YaVersion.CAMCORDER_COMPONENT_VERSION,
40 description =
"A component to record a video using the device's camcorder." +
41 "After the video is recorded, the name of the file on the phone " +
42 "containing the clip is available as an argument to the " +
43 "AfterRecording event. The file name can be used, for example, to set " +
44 "the source property of a VideoPlayer component.",
45 category = ComponentCategory.MEDIA,
47 iconName =
"images/camcorder.png")
49 @UsesPermissions(permissionNames =
"android.permission.WRITE_EXTERNAL_STORAGE, android.permission.READ_EXTERNAL_STORAGE," +
50 "android.permission.CAMERA")
54 private static final String CAMCORDER_INTENT =
"android.media.action.VIDEO_CAPTURE";
59 private int requestCode;
62 private boolean havePermission =
false;
70 super(container.
$form());
71 this.container = container;
79 String state = Environment.getExternalStorageState();
80 if (!havePermission) {
82 form.runOnUiThread(
new Runnable() {
85 form.askPermission(Manifest.permission.CAMERA,
88 public void HandlePermissionResponse(String permission,
boolean granted) {
90 me.havePermission =
true;
93 form.dispatchPermissionDeniedEvent(me,
"RecordVideo",
94 Manifest.permission.CAMERA);
103 if (Environment.MEDIA_MOUNTED.equals(state)) {
104 Log.i(
"CamcorderComponent",
"External storage is available and writable");
106 if (requestCode == 0) {
107 requestCode = form.registerForActivityResult(
this);
110 Intent intent =
new Intent(CAMCORDER_INTENT);
111 container.
$context().startActivityForResult(intent, requestCode);
112 }
else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
113 form.dispatchErrorOccurredEvent(
this,
"RecordVideo",
116 form.dispatchErrorOccurredEvent(
this,
"RecordVideo",
123 Log.i(
"CamcorderComponent",
124 "Returning result. Request code = " + requestCode +
", result code = " + resultCode);
125 if (requestCode == this.requestCode && resultCode == Activity.RESULT_OK) {
126 if (data !=
null && data.getData() !=
null) {
127 Uri tryClipUri = data.getData();
128 Log.i(
"CamcorderComponent",
"Calling Camcorder.AfterPicture with clip path "
129 + tryClipUri.toString());
130 AfterRecording(tryClipUri.toString());
132 Log.i(
"CamcorderComponent",
"Couldn't find a clip file from the Camcorder result");
133 form.dispatchErrorOccurredEvent(
this,
"TakeVideo",
137 Log.i(
"CamcorderComponent",
"No clip filed rerturn; request failed");
138 form.dispatchErrorOccurredEvent(
this,
"TakeVideo",
143 private void deleteFile(Uri fileUri) {
144 File fileToDelete =
new File(fileUri.getPath());
146 if (fileToDelete.delete()) {
147 Log.i(
"CamcorderComponent",
"Deleted file " + fileUri.toString());
149 Log.i(
"CamcorderComponent",
"Could not delete file " + fileUri.toString());
151 }
catch (SecurityException e) {
152 Log.i(
"CamcorderComponent",
"Got security exception trying to delete file "
153 + fileUri.toString());