我知道这里有很多关于Phonegap和使用相机功能的问题。 我试图解决问题,但仍然没有解决方案。
设备是Nexus 7,相机在构建应用程序后无法启动。 这是代码:
<script type="text/javascript" src="PhoneGap.js" charset="utf-8"></script>
<script type="text/javascript" charset="utf-8">
var pictureSource; // picture source
var destinationType; // sets the format of returned value
// Wait for PhoneGap to connect with the device
//
document.addEventListener("deviceready",onDeviceReady,false);
// PhoneGap 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 });
}
// 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 });
}
// 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>
用三星Galaxy s2试了一次......再一次没事! 我不知道这个问题!? 这是代码:
androidmainfest.xml权限:
<uses-sdk android:minSdkVersion="14" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.RECORD_VIDEO"/>
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.BROADCAST_STICKY" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
,这里是html文件中的代码:
<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 PhoneGap to connect with the device
//
document.addEventListener("deviceready",onDeviceReady,false);
// PhoneGap 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 });
}
// 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 });
}
// 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>
答案 0 :(得分:0)
您需要提供文档中提到的选项。例如:
navigator.camera.getPicture(onSuccess, onFail, { quality: 50,
destinationType: Camera.DestinationType.DATA_URL
});
Rereference: http://docs.phonegap.com/en/2.5.0/cordova_camera_camera.md.html#camera.getPicture
答案 1 :(得分:0)
现在它有效。不知道问题,但它确实有效。 这是代码:
<!DOCTYPE html>
<html>
<head>
<title>Capture Photo</title>
<link rel="stylesheet" href="css/style.css" />
<script type="text/javascript" charset="utf-8" src="cordova-2.5.0.js"></script>
<script type="text/javascript" charset="utf-8">
function capturePhoto(){
navigator.camera.getPicture(uploadPhoto,null,{sourceType:1,quality:60});
}
function uploadPhoto(data){
// this is where you would send the image file to server
//output image to screen
cameraPic.src = "data:image/jpeg;base64," + data;
}
navigator.notification.alert(message, alertCallback, [title], [buttonName]);
navigator.notification.alert(
'Your Photo has been uploaded', // message
okay, // callback
'Photo Uploaded', // title
'OK' // buttonName
);
function uploadPhoto(data){
// this is where you would send the image file to server
cameraPic.src = "data:image/jpeg;base64," + data;
// Successful upload to the server
navigator.notification.alert(
'Your Photo has been uploaded', // message
okay, // callback
'Photo Uploaded', // title
'OK' // buttonName
);
// upload has failed Fail
/*
if (failedToUpload){
navigator.notification.alert(
'Your Photo has failed to upload',
failedDismissed,
'Photo Not Uploaded',
'OK'
);
}
*/
}
function okay(){
// Do something
}
</script>
</head>
<body>
<div id="camera">
<button class="camera-control" onclick="capturePhoto();">Capture Photo</button>
<div style="text-align:center;margin:20px;">
<img id="cameraPic" src="" style="width:auto;height:120px;"></img>
</div>
</div>
</body>
</html>
答案 2 :(得分:0)
这是我的建议
1.更换cramer插件,避免使用自定义插件启动垃圾回收,(http://code.google.com/p/foreground-camera-plugin/,http://code.google.com/p/foreground-gallery-插件/) 2.如果不足以杀死其他进程,则检测内存,活动释放内存 3.提高生存率,尽量避免在所选系统中释放内存。
private void clearMemory(boolean killAll)
{
mklog("当前系统可用内存大小是:" + getAvailMemory(getApplicationContext()));
ActivityManager activityManger = (ActivityManager) this
.getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> list = activityManger
.getRunningAppProcesses();
if (list != null)
for (int i = 0; i < list.size(); i++)
{
ActivityManager.RunningAppProcessInfo apinfo = list.get(i);
System.out.println("pid " + apinfo.pid);
System.out.println("processName "
+ apinfo.processName);
System.out
.println("importance " + apinfo.importance);
String[] pkgList = apinfo.pkgList;
if (apinfo.importance >= ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND
|| (killAll && apinfo.importance >= ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE))
{
// Process.killProcess(apinfo.pid);
for (int j = 0; j < pkgList.length; j++)
{
activityManger.killBackgroundProcesses(pkgList[j]);
mklog("准备杀死进程:" + pkgList[j]);
}
}
}
mklog("清理之后 当前系统可用内存大小是:" + getAvailMemory(getApplicationContext()));
}
private long getAvailMemory(Context context)
{
ActivityManager am = (ActivityManager) context
.getSystemService(Context.ACTIVITY_SERVICE);
MemoryInfo mi = new MemoryInfo();
am.getMemoryInfo(mi);
return mi.availMem / (1024 * 1024);
}
public void mklog(String contentString)
{
Log.i("Web Console", contentString);
}