我创建了一个Android模块来调用摄像头并将视频大小限制为15MB。它适用于所有设备。但是在s5上它会在到达onResult回调之前崩溃.Logcat错误和代码附在下面。
代码
/**
* This file was auto-generated by the Titanium Module SDK helper for Android
* Appcelerator Titanium Mobile
* Copyright (c) 2009-2010 by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*
*/
package com.ex.videomodule;
import org.appcelerator.kroll.KrollDict;
import org.appcelerator.kroll.KrollProxy;
import org.appcelerator.kroll.annotations.Kroll;
import org.appcelerator.titanium.TiC;
import org.appcelerator.titanium.util.Log;
import org.appcelerator.titanium.util.TiConfig;
import org.appcelerator.titanium.util.TiConvert;
import org.appcelerator.titanium.proxy.TiViewProxy;
import org.appcelerator.titanium.view.TiCompositeLayout;
import org.appcelerator.titanium.view.TiCompositeLayout.LayoutArrangement;
import org.appcelerator.titanium.view.TiUIView;
import org.appcelerator.titanium.TiApplication;
import org.appcelerator.titanium.util.TiActivityResultHandler;
import org.appcelerator.titanium.util.TiActivitySupport;
import android.app.Activity;
import android.os.Environment;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.File;
import android.net.Uri;
import android.content.Intent;
import android.provider.MediaStore;
import org.appcelerator.kroll.KrollFunction;
import java.util.HashMap;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.DateFormat;
import java.util.Date;
// This proxy can be created by calling VideoModule.createExample({message: "hello world"})
@Kroll.proxy(creatableInModule=VideoModuleModule.class)
public class ExampleProxy extends TiViewProxy implements TiActivityResultHandler
{
// Standard Debugging variables
private static final String LCAT = "ExampleProxy";
private static final boolean DBG = TiConfig.LOGD;
private int REQ_CODE_CAPTURE_VIDEO = 1;
Uri mVideoUri;
String mVideoPath;
protected KrollFunction successCallback;
protected KrollFunction errorCallback;
private class ExampleView extends TiUIView
{
public ExampleView(TiViewProxy proxy) {
super(proxy);
LayoutArrangement arrangement = LayoutArrangement.DEFAULT;
if (proxy.hasProperty(TiC.PROPERTY_LAYOUT)) {
String layoutProperty = TiConvert.toString(proxy.getProperty(TiC.PROPERTY_LAYOUT));
if (layoutProperty.equals(TiC.LAYOUT_HORIZONTAL)) {
arrangement = LayoutArrangement.HORIZONTAL;
} else if (layoutProperty.equals(TiC.LAYOUT_VERTICAL)) {
arrangement = LayoutArrangement.VERTICAL;
}
}
setNativeView(new TiCompositeLayout(proxy.getActivity(), arrangement));
}
@Override
public void processProperties(KrollDict d)
{
super.processProperties(d);
}
}
// Constructor
public ExampleProxy()
{
super();
}
@Override
public TiUIView createView(Activity activity)
{
TiUIView view = new ExampleView(this);
view.getLayoutParams().autoFillsHeight = true;
view.getLayoutParams().autoFillsWidth = true;
return view;
}
// Handle creation options
@Override
public void handleCreationDict(KrollDict options)
{
super.handleCreationDict(options);
if (options.containsKey("message")) {
Log.d(LCAT, "example created with message: " + options.get("message"));
}
}
// Methods
@Kroll.method
public void printMessage(String message)
{
Log.d(LCAT, "printing message: " + message);
}
@Kroll.getProperty @Kroll.method
public String getMessage()
{
return "Hello World from my module";
}
@Kroll.setProperty @Kroll.method
public void setMessage(String message)
{
Log.d(LCAT, "Tried setting module message to: " + message);
}
@Kroll.method
public void registerCallback(HashMap options){
Log.d(LCAT, "inside onAppCreate");
successCallback = (KrollFunction) options.get("callback");
errorCallback = (KrollFunction) options.get("errorCallback");
}
@Kroll.method
private void captureVideo(String path){
mVideoPath = path;
File mediaFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath()
+mVideoPath);
Intent intent = new Intent(android.provider.MediaStore.ACTION_VIDEO_CAPTURE);
mVideoUri = Uri.fromFile(mediaFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, mVideoUri);
intent.putExtra("android.intent.extra.videoQuality", 0);
intent.putExtra("android.intent.extra.sizeLimit",15728640L);
Activity activity = TiApplication.getInstance().getCurrentActivity();
TiActivitySupport support = (TiActivitySupport) activity;
support.launchActivityForResult(intent, REQ_CODE_CAPTURE_VIDEO, this);
}
@Override
public void onResult(Activity activity, int thisRequestCode, int resultCode, Intent data)
{
Log.d(LCAT, "onResult "+resultCode);
if (resultCode == 0) {
HashMap map = new HashMap();
map.put("message", "Cancelled");
errorCallback.call(getKrollObject(), map);
} else {
if (mVideoUri != null) {
Log.d(LCAT, "onResult videro captured " + mVideoUri.getPath());
File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+mVideoPath);
if (f.exists() && !f.isDirectory()) {
Log.d(LCAT, "onResult File exists " + Environment.getExternalStorageDirectory().getAbsolutePath()+mVideoPath);
HashMap data1 = new HashMap();
data1.put("video",Environment.getExternalStorageDirectory().getAbsolutePath()+ mVideoPath);
successCallback.call(getKrollObject(), data1);
} else {
mVideoUri = null;
mVideoPath = null;
HashMap map = new HashMap();
map.put("message", "Failed To load Image 1");
errorCallback.call(getKrollObject(), map);
}
} else {
mVideoUri = null;
mVideoPath = null;
HashMap map = new HashMap();
map.put("message", "Failed To load Image 2");
errorCallback.call(getKrollObject(), map);
}
}
Log.d(LCAT, "onResult videro captured"+resultCode);
}
@Override
public void onError(Activity activity, int requestCode, Exception e)
{
Log.d(LCAT, "onError "+ e.getMessage());
HashMap map = new HashMap();
map.put("message", "Cancelled");
errorCallback.call(getKrollObject(), map);
}
}
LOG
> java.lang.RuntimeException: Unable to destroy activity
> {com.ex.sample/org.appcelerator.titanium.TiActivity}:
> java.lang.NullPointerException: Attempt to invoke virtual method 'void
> android.view.OrientationEventListener.disable()' on a null object
> reference at
> android.app.ActivityThread.performDestroyActivity(ActivityThread.java:4893)
> at
> android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:4911)
> at android.app.ActivityThread.access$1600(ActivityThread.java:210)
> at
> android.app.ActivityThread$H.handleMessage(ActivityThread.java:1758)
> at android.os.Handler.dispatchMessage(Handler.java:102) at
> android.os.Looper.loop(Looper.java:145) at
> android.app.ActivityThread.main(ActivityThread.java:6938) at
> java.lang.reflect.Method.invoke(Native Method) at
> java.lang.reflect.Method.invoke(Method.java:372) at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
> at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
> Caused by: java.lang.NullPointerException: Attempt to invoke virtual
> method 'void android.view.OrientationEventListener.disable()' on a
> null object reference at
> org.appcelerator.titanium.TiBaseActivity.onDestroy(TiBaseActivity.java:1373)
> at org.appcelerator.titanium.TiActivity.onDestroy(TiActivity.java:29)
> at android.app.Activity.performDestroy(Activity.java:6809) at
> android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1169)
> at
> android.app.ActivityThread.performDestroyActivity(ActivityThread.java:4871)
> ... 10 more
有时会遇到崩溃
java.lang.NullPointerException:尝试调用虚方法'void null对象上的android.widget.LinearLayout.removeAllViews()' 参考 ti.modules.titanium.ui.widget.TiUIActivityIndicator.setStyle(TiUIActivityIndicator.java:164) 在 ti.modules.titanium.ui.widget.TiUIActivityIndicator.processProperties(TiUIActivityIndicator.java:86) 在 org.appcelerator.kroll.KrollProxy.setModelListener(KrollProxy.java:1209) 在 org.appcelerator.titanium.proxy.TiViewProxy.realizeViews(TiViewProxy.java:502) 在 org.appcelerator.titanium.proxy.TiViewProxy.handleGetView(TiViewProxy.java:493) 在 org.appcelerator.titanium.proxy.TiViewProxy.getOrCreateView(TiViewProxy.java:471) 在 ti.modules.titanium.ui.ActivityIndicatorProxy.handleHide(ActivityIndicatorProxy.java:94) 在 org.appcelerator.titanium.proxy.TiViewProxy.handleMessage(TiViewProxy.java:259) 在 ti.modules.titanium.ui.ActivityIndicatorProxy.handleMessage(ActivityIndicatorProxy.java:67) 在android.os.Handler.dispatchMessage(Handler.java:98)处 android.os.Looper.loop(Looper.java:145)at android.app.ActivityThread.main(ActivityThread.java:5835)at java.lang.reflect.Method.invoke(Native Method)at java.lang.reflect.Method.invoke(Method.java:372)at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1399) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)