我坚持这几天我真的不知道如何解决它我已经尝试过整个互联网但没有代码似乎对我有用。 我想做的是拍照并将图像发送到我的服务器。 我这样做的方式是:我调用navigator.camera.getPicture方法拍摄照片,如果没有问题发生,我将它发送到服务器,但是即使调用了错误的回调也是如此拍摄照片并存储在设备上。 这是我的index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="format-detection" content="telephone=no" />
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
<link rel="stylesheet" type="text/css" href="css/index.css" />
<title>Camera</title>
</head>
<body>
<script type="text/javascript" src="cordova-2.7.0.js"></script>
<script type="text/javascript" src="js/index.js"></script>
<script type="text/javascript">
// Initialize the application
app.initialize();
// Listen for 'touchpad' events, which get registered as keydown events with keyCode 9 and 13
document.addEventListener('keydown',keyController);
// Wait for PhoneGap to load
document.addEventListener("deviceready", onDeviceReady, false);
// PhoneGap is ready
function onDeviceReady() {}
// Called if something bad happens.
function captureError(error)
{
console.log("ERROOOOOOOOOOOOOOOOOOOOOOOR");
}
function captureImage()
{
navigator.camera.getPicture(onPhotoUriSuccess, captureError, { quality: 50, destinationType: navigator.camera.DestinationType.FILE_URI });
}
function onPhotoUriSuccess(imageUriToUpload)
{
var url=encodeURI("http://ec2-54-186-34-253.us-west-2.compute.amazonaws.com/upload.php");
var options = new FileUploadOptions();
options.fileName = imageUriToUpload.substr(imageUriToUpload.lastIndexOf('/')+1);
options.mimeType = "image/jpeg";
options.chunkedMode = true; //this is important to send both data and files
var ft = new FileTransfer();
ft.upload(imageUriToUpload, url, function(success){console.log("sucess")}, function(error){console.log("error")}, options);
}
//Takes care of touch input
function keyController(e)
{
// Show results in document body
//document.body.innerHTML = e.keyCode;
console.log("keyController");
captureImage();
}
// Show results in document body
function onError()
{
document.body.innerHTML = 'error';
}
</script>
</body>
</html>
我忘记发布错误:
04-13 00:48:32.188: E/JHEAD(8586): can't open '/mnt/sdcard/Android/data/org.apache.cordova.example/cache/.Pic.jpg'
04-13 00:48:32.195: W/System.err(8586): java.io.FileNotFoundException: /mnt/sdcard/Android/data/org.apache.cordova.example/cache/.Pic.jpg: open failed: ENOENT (No such file or directory)
04-13 00:48:32.195: W/System.err(8586): at libcore.io.IoBridge.open(IoBridge.java:406)
04-13 00:48:32.195: W/System.err(8586): at java.io.FileInputStream.<init>(FileInputStream.java:78)
04-13 00:48:32.195: W/System.err(8586): at java.io.FileInputStream.<init>(FileInputStream.java:105)
04-13 00:48:32.195: W/System.err(8586): at org.apache.cordova.FileHelper.getInputStreamFromUriString(FileHelper.java:101)
04-13 00:48:32.195: W/System.err(8586): at org.apache.cordova.CameraLauncher.getScaledBitmap(CameraLauncher.java:582)
04-13 00:48:32.195: W/System.err(8586): at org.apache.cordova.CameraLauncher.onActivityResult(CameraLauncher.java:335)
04-13 00:48:32.195: W/System.err(8586): at org.apache.cordova.DroidGap.onActivityResult(DroidGap.java:864)
04-13 00:48:32.195: W/System.err(8586): at android.app.Activity.dispatchActivityResult(Activity.java:4654)
04-13 00:48:32.195: W/System.err(8586): at android.app.ActivityThread.deliverResults(ActivityThread.java:2976)
04-13 00:48:32.203: W/System.err(8586): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3023)
04-13 00:48:32.203: W/System.err(8586): at android.app.ActivityThread.access$1100(ActivityThread.java:123)
04-13 00:48:32.203: W/System.err(8586): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1177)
04-13 00:48:32.203: W/System.err(8586): at android.os.Handler.dispatchMessage(Handler.java:99)
04-13 00:48:32.203: W/System.err(8586): at android.os.Looper.loop(Looper.java:137)
04-13 00:48:32.211: W/System.err(8586): at android.app.ActivityThread.main(ActivityThread.java:4424)
04-13 00:48:32.211: W/System.err(8586): at java.lang.reflect.Method.invokeNative(Native Method)
04-13 00:48:32.211: W/System.err(8586): at java.lang.reflect.Method.invoke(Method.java:511)
04-13 00:48:32.211: W/System.err(8586): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-13 00:48:32.211: W/System.err(8586): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-13 00:48:32.211: W/System.err(8586): at dalvik.system.NativeStart.main(Native Method)
04-13 00:48:32.219: W/System.err(8586): Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
04-13 00:48:32.227: W/System.err(8586): at libcore.io.Posix.open(Native Method)
04-13 00:48:32.227: W/System.err(8586): at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
04-13 00:48:32.234: W/System.err(8586): at libcore.io.IoBridge.open(IoBridge.java:390)
04-13 00:48:32.234: W/System.err(8586): ... 19 more
答案 0 :(得分:0)
也许,您在权限列表中缺少WRITE_EXTERNAL_STORAGE
。没有它,应用程序将无法编写jpg文件。
我遇到了问题,我为Android Phonegap 2.2 Error compressing image的答案提供了资金 祝你好运