Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfixes - Video Capture for A4.3 && Aync IO #8

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 63 additions & 24 deletions src/android/Capture.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ Licensed to the Apache Software Foundation (ASF) under one
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

import android.os.Build;

Expand Down Expand Up @@ -241,6 +244,7 @@ private void captureVideo(int duration) {
if(Build.VERSION.SDK_INT > 7){
intent.putExtra("android.intent.extra.durationLimit", duration);
}

this.cordova.startActivityForResult((CordovaPlugin) this, intent, CAPTURE_VIDEO);
}

Expand Down Expand Up @@ -322,8 +326,20 @@ public void onActivityResult(int requestCode, int resultCode, Intent intent) {
this.fail(createErrorObject(CAPTURE_INTERNAL_ERR, "Error capturing image."));
}
} else if (requestCode == CAPTURE_VIDEO) {
// Get the uri of the video clip
Uri data = intent.getData();

Uri data = null;
if (intent != null)
{
// Get the uri of the video clip
data = intent.getData();
}

if (data == null)
{
File movie = new File(getTempDirectoryPath(), "Capture.avi");
data = Uri.fromFile(movie);
}

// create a file object from the uri
if(data == null)
{
Expand Down Expand Up @@ -374,35 +390,58 @@ else if (resultCode == Activity.RESULT_CANCELED) {
* @return a JSONObject that represents a File
* @throws IOException
*/
private JSONObject createMediaFile(Uri data) {
File fp = webView.getResourceApi().mapUriToFile(data);
JSONObject obj = new JSONObject();
private JSONObject createMediaFile(final Uri data) {
Future<JSONObject> result = cordova.getThreadPool().submit(new Callable<JSONObject>()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

createMediaFile is just a helper method, so it shouldn't do any multithreading. Instead, you should put the async logic into the caller. This is done already except for in onActivityResult().

{
@Override
public JSONObject call() throws Exception
{
File fp = webView.getResourceApi().mapUriToFile(data);
JSONObject obj = new JSONObject();

try {
// File properties
obj.put("name", fp.getName());
obj.put("fullPath", fp.toURI().toString());
// Because of an issue with MimeTypeMap.getMimeTypeFromExtension() all .3gpp files
// are reported as video/3gpp. I'm doing this hacky check of the URI to see if it
// is stored in the audio or video content store.
if (fp.getAbsoluteFile().toString().endsWith(".3gp") || fp.getAbsoluteFile().toString().endsWith(".3gpp")) {
if (data.toString().contains("/audio/")) {
obj.put("type", AUDIO_3GPP);
} else {
obj.put("type", VIDEO_3GPP);
try {
// File properties
obj.put("name", fp.getName());
obj.put("fullPath", fp.toURI().toString());
// Because of an issue with MimeTypeMap.getMimeTypeFromExtension() all .3gpp files
// are reported as video/3gpp. I'm doing this hacky check of the URI to see if it
// is stored in the audio or video content store.
if (fp.getAbsoluteFile().toString().endsWith(".3gp") || fp.getAbsoluteFile().toString().endsWith(".3gpp")) {
if (data.toString().contains("/audio/")) {
obj.put("type", AUDIO_3GPP);
} else {
obj.put("type", VIDEO_3GPP);
}
} else {
obj.put("type", FileHelper.getMimeType(Uri.fromFile(fp), cordova));
}

obj.put("lastModifiedDate", fp.lastModified());
obj.put("size", fp.length());
} catch (JSONException e) {
// this will never happen
e.printStackTrace();
}
} else {
obj.put("type", FileHelper.getMimeType(Uri.fromFile(fp), cordova));

return obj;
}
});

obj.put("lastModifiedDate", fp.lastModified());
obj.put("size", fp.length());
} catch (JSONException e) {
// this will never happen

try
{
return result.get();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
catch (ExecutionException e)
{
e.printStackTrace();
}

return obj;
return null;
}

private JSONObject createErrorObject(int code, String message) {
Expand Down