Androidフォンで写真をキャプチャするためのphonegapアプリを作成しています。Android 4.x では問題なく動作します。Android 2.3.xでは機能しません
次のようなエラーが発生しました。
ごめん!アプリケーション AppName(packagename) が予期せず停止しました。もう一度お試しください
ここで述べたようにプロジェクトを作成し、ここで完全な例を使用しました。
私の phonegep html ページは次のとおりです。
<!DOCTYPE html>
<html>
<head>
<title>Capture Photo</title>
<script type="text/javascript" charset="utf-8" src="cordova-2.5.0.js"></script>
<script type="text/javascript" charset="utf-8">
var pictureSource; // picture source
var destinationType; // sets the format of returned value
// Wait for Cordova to connect with the device
//
document.addEventListener("deviceready",onDeviceReady,false);
// Cordova is ready to be used!
//
function onDeviceReady() {
pictureSource=navigator.camera.PictureSourceType;
destinationType=navigator.camera.DestinationType;
}
// Called when a photo is successfully retrieved
//
function onPhotoDataSuccess(imageData) {
// Uncomment to view the base64 encoded image data
// console.log(imageData);
// Get image handle
//
var smallImage = document.getElementById('smallImage');
// Unhide image elements
//
smallImage.style.display = 'block';
// Show the captured photo
// The inline CSS rules are used to resize the image
//
smallImage.src = "data:image/jpeg;base64," + imageData;
}
// Called when a photo is successfully retrieved
//
function onPhotoURISuccess(imageURI) {
// Uncomment to view the image file URI
// console.log(imageURI);
// Get image handle
//
var largeImage = document.getElementById('largeImage');
// Unhide image elements
//
largeImage.style.display = 'block';
// Show the captured photo
// The inline CSS rules are used to resize the image
//
largeImage.src = imageURI;
}
// A button will call this function
//
function capturePhoto() {
// Take picture using device camera and retrieve image as base64-encoded string
navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 50,
destinationType: destinationType.DATA_URL });
}
// A button will call this function
//
function capturePhotoEdit() {
// Take picture using device camera, allow edit, and retrieve image as base64-encoded string
navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 20, allowEdit: true,
destinationType: destinationType.DATA_URL });
}
// A button will call this function
//
function getPhoto(source) {
// Retrieve image file location from specified source
navigator.camera.getPicture(onPhotoURISuccess, onFail, { quality: 50,
destinationType: destinationType.FILE_URI,
sourceType: source });
}
// Called if something bad happens.
//
function onFail(message) {
alert('Failed because: ' + message);
}
</script>
</head>
<body>
<button onclick="capturePhoto();">Capture Photo</button> <br>
<button onclick="capturePhotoEdit();">Capture Editable Photo</button> <br>
<button onclick="getPhoto(pictureSource.PHOTOLIBRARY);">From Photo Library</button><br>
<button onclick="getPhoto(pictureSource.SAVEDPHOTOALBUM);">From Photo Album</button><br>
<img style="display:none;width:60px;height:60px;" id="smallImage" src="" />
<img style="display:none;" id="largeImage" src="" />
</body>
</html>
エラーログ:
05-16 14:51:38.249: E/AndroidRuntime(13459): FATAL EXCEPTION: main
05-16 14:51:38.249: E/AndroidRuntime(13459): java.lang.RuntimeException: Unable to resume activity {com.name/package}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=34, result=-1, data=Intent { (has extras) }} to activity {com.name/com.name.namedemo}: java.lang.NullPointerException
05-16 14:51:38.249: E/AndroidRuntime(13459): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2120)
05-16 14:51:38.249: E/AndroidRuntime(13459): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2135)
05-16 14:51:38.249: E/AndroidRuntime(13459): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1668)
05-16 14:51:38.249: E/AndroidRuntime(13459): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-16 14:51:38.249: E/AndroidRuntime(13459): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
05-16 14:51:38.249: E/AndroidRuntime(13459): at android.os.Handler.dispatchMessage(Handler.java:99)
05-16 14:51:38.249: E/AndroidRuntime(13459): at android.os.Looper.loop(Looper.java:130)
05-16 14:51:38.249: E/AndroidRuntime(13459): at android.app.ActivityThread.main(ActivityThread.java:3683)
05-16 14:51:38.249: E/AndroidRuntime(13459): at java.lang.reflect.Method.invokeNative(Native Method)
05-16 14:51:38.249: E/AndroidRuntime(13459): at java.lang.reflect.Method.invoke(Method.java:507)
05-16 14:51:38.249: E/AndroidRuntime(13459): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:875)
05-16 14:51:38.249: E/AndroidRuntime(13459): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:633)
05-16 14:51:38.249: E/AndroidRuntime(13459): at dalvik.system.NativeStart.main(Native Method)
05-16 14:51:38.249: E/AndroidRuntime(13459): Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=34, result=-1, data=Intent { (has extras) }} to activity {com.name/com.name.namedemo}: java.lang.NullPointerException
05-16 14:51:38.249: E/AndroidRuntime(13459): at android.app.ActivityThread.deliverResults(ActivityThread.java:2532)
05-16 14:51:38.249: E/AndroidRuntime(13459): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2107)
05-16 14:51:38.249: E/AndroidRuntime(13459): ... 12 more
05-16 14:51:38.249: E/AndroidRuntime(13459): Caused by: java.lang.NullPointerException
05-16 14:51:38.249: E/AndroidRuntime(13459): at org.apache.cordova.DroidGap.onActivityResult(DroidGap.java:849)
05-16 14:51:38.249: E/AndroidRuntime(13459): at android.app.Activity.dispatchActivityResult(Activity.java:3908)
05-16 14:51:38.249: E/AndroidRuntime(13459): at android.app.ActivityThread.deliverResults(ActivityThread.java:2528)
05-16 14:51:38.249: E/AndroidRuntime(13459): ... 13 more
05-16 14:51:39.039: W/webcore(13459): Can't get the viewWidth after the first layout