I\'ve made a relatively simple phonegap app, with the ability to capture images and videos and upload them to a server.
Images work fine, however when I call to capt
I think a better solution would be to actually prevent using the UI thread, instead of disabling the Thread Checking.
I solved it like this:
private JSONObject createMediaFile(final Uri data) {
Future<JSONObject> result = cordova.getThreadPool().submit(new Callable<JSONObject>()
{
@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);
}
} 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();
}
return obj;
}
});
try
{
return result.get();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
catch (ExecutionException e)
{
e.printStackTrace();
}
return null;
I wrapped the default method in a callable inner class. Now it gets executed in another thread.
A workaround for this issue is to edit [yourApp]\plugins\org.apache.cordova.media-capture\src\android\Capture.java
Starting at line 377, change this code
private JSONObject createMediaFile(Uri data) {
File fp = webView.getResourceApi().mapUriToFile(data);
JSONObject obj = new JSONObject();
to the following code
private JSONObject createMediaFile(Uri data) {
// is thread checking enabled?
boolean thC = webView.getResourceApi().isThreadCheckingEnabled();
// set thread checking to disabled (this works around mapUriToFile failing with IllegalStateException: Do not perform IO operations on the UI thread.
webView.getResourceApi().setThreadCheckingEnabled(false);
File fp = webView.getResourceApi().mapUriToFile(data);
// set thread checking back to whatever it was before the call above.
webView.getResourceApi().setThreadCheckingEnabled(thC);
JSONObject obj = new JSONObject();
This worked for us, hopefully it will be fixed properly soon.