2015-06-03 18 views
11

Tôi đang phát triển một ứng dụng di động với cordova/phonegap và tôi đã cài đặt plugin Camera. Tôi có thể mở máy ảnh và nhấp vào hình ảnh nhưng sau khi ứng dụng đó gặp sự cố. đây là nhật ký sự cố:PhoneGap App Crash khi chụp ảnh mới bằng Camera Plugin

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=34, result=-1, data=null} to activity {com.phonegap.helloworld/com.phonegap.helloworld.CordovaApp}: java.lang.IllegalArgumentException: filename cannot be null 
    E/AndroidRuntime(22226): at android.app.ActivityThread.deliverResults(ActivityThread.java:3510) 
    E/AndroidRuntime(22226): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3553) 
    E/AndroidRuntime(22226): at android.app.ActivityThread.access$1200(ActivityThread.java:165) 
    E/AndroidRuntime(22226): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1374) 
    E/AndroidRuntime(22226): at android.os.Handler.dispatchMessage(Handler.java:99) 
    E/AndroidRuntime(22226): at android.os.Looper.loop(Looper.java:176) 
    E/AndroidRuntime(22226): at android.app.ActivityThread.main(ActivityThread.java:5455) 
    E/AndroidRuntime(22226): at java.lang.reflect.Method.invokeNative(Native Method) 
    E/AndroidRuntime(22226): at java.lang.reflect.Method.invoke(Method.java:525) 
    E/AndroidRuntime(22226): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209) 
    E/AndroidRuntime(22226): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025) 
    E/AndroidRuntime(22226): at dalvik.system.NativeStart.main(Native Method) 
    E/AndroidRuntime(22226): Caused by: java.lang.IllegalArgumentException: filename cannot be null 
    E/AndroidRuntime(22226): at android.media.ExifInterface.<init>(ExifInterface.java:121) 
    E/AndroidRuntime(22226): at org.apache.cordova.camera.ExifHelper.createOutFile(ExifHelper.java:66) 
    E/AndroidRuntime(22226): at org.apache.cordova.camera.CameraLauncher.processResultFromCamera(CameraLauncher.java:430) 
    E/AndroidRuntime(22226): at org.apache.cordova.camera.CameraLauncher.onActivityResult(CameraLauncher.java:610) 
    E/AndroidRuntime(22226): at org.apache.cordova.CordovaActivity.onActivityResult(CordovaActivity.java:784) 
    E/AndroidRuntime(22226): at android.app.Activity.dispatchActivityResult(Activity.java:5563) 
    E/AndroidRuntime(22226): at android.app.ActivityThread.deliverResults(ActivityThread.java:3506) 
    E/AndroidRuntime(22226): ... 11 more 

mã mà tôi đã sử dụng để mở camera là:

var pictureSource; // picture source 
var destinationType; // sets the format of returned value 
document.addEventListener("deviceready", onDeviceReady, false); 

function onDeviceReady() { 
    // alert("ready-----") 
    pictureSource = navigator.camera.PictureSourceType; 
    destinationType = navigator.camera.DestinationType; 
} 

function capturePhoto() { 
    alert(navigator.camera); 
    navigator.camera.getPicture(onPhotoDataSuccess, onFail, { 
    quality: 20, 
    destinationType: destinationType.FILE_URI, 
    targetWidth: 200, 
    targetHeight: 200, 
    saveToPhotoAlbum: true, 
    sourceType: pictureSource.CAMERA 
    }); 
} 

function onPhotoDataSuccess(imageURI) { 
    // alert("success--"); 
    var smallImage = document.getElementById('smallImage'); 
    smallImage.style.display = 'block'; 
    smallImage.src = imageURI; 
    // smallImage.src = "data:image/jpeg;base64," + imageData; 
} 

function onFail(message) { 
    alert('Failed because: ' + message); 
} 

Các lỗi đã được đăng nhập Apache Cordova.

Trợ giúp !!!

+0

mẫu mã của bạn cũng giống như trong 'tài liệu Phonegap's'. Nó cũng tương tự như tài liệu 'Cordova's', nơi khác biệt duy nhất là dòng này:' destinationType: Camera.DestinationType.FILE_URI, sourceType: Camera.pictureSource.CAMERA' trong đối tượng tùy chọn. Bạn đã thử những thay vào đó? Bạn đang sử dụng phiên bản cordova hoặc phonegap nào? – JcDenton86

Trả lời

1

hãy thay đổi kiểu điểm đến của bạn:

destinationType: destinationType.FILE_URI 

để

destinationType: Camera.DestinationType.FILE_URI 

nếu bạn cần nó như là một hình ảnh base64 mã hóa, sau đó thay đổi loại đích như:

destinationType: Camera.DestinationType.DATA_URL 

cũng đảm bảo rằng bạn đã thêm quyền ghi lưu trữ bên ngoài vào tệp kê khai của mình.

+0

Tôi cần hình ảnh trong tệp không phải là chuỗi nhị phân. –

+0

bạn có thay đổi loại đích của mình không? với việc sử dụng destinationType: Camera.DestinationType.FILE_URI, bạn sẽ nhận được vị trí tệp của hình ảnh. –

+0

Có thể bạn có thể loại bỏ chiều cao mục tiêu và tham số chiều rộng mục tiêu chỉ cần cung cấp cho nó một thử nó sẽ xảy ra với tôi quá .. –

2

Hãy thử thay thế chức năng capturePhoto() của bạn với điều này (điều này đang làm việc kể từ ứng dụng của riêng tôi):

function capturePhoto() { 
    var options = { 
     quality: 75, 
     destinationType: Camera.DestinationType.FILE_URI, 
     sourceType: Camera.PictureSourceType.CAMERA, 
     mediaType: Camera.MediaType.CAMERA, 
     encodingType: Camera.EncodingType.JPEG, 
     targetWidth: 200, 
     targetHeight: 200, 
     saveToPhotoAlbum: true 
    }; 
    navigator.camera.getPicture(onPhotoDataSuccess, onFail, options); 
} 
+2

không hoạt động. –

+0

Tôi gặp sự cố tương tự trên thiết bị Android của mình và hóa ra loại mã hóa của tôi đã được đặt thành PNG. Thay đổi nó thành JPEG đã giải quyết vấn đề này cho tôi – Kevin

0

Các plugin được đi qua một con trỏ null để ExifInterface gây một ngoại lệ, đó đã không xảy ra trong các plugin cũ hơn.

Thử áp dụng bản sửa lỗi sau, bạn cũng có thể báo cáo phiên bản thiết bị, Mô hình và phiên bản Android của thiết bị không?

https://issues.apache.org/jira/browse/CB-9446

diff --git a/src/android/ExifHelper.java b/src/android/ExifHelper.java 
index 5160a2f..0af0fcd 100644 
--- a/src/android/ExifHelper.java 
+++ b/src/android/ExifHelper.java 
@@ -53,7 +53,11 @@ public class ExifHelper { 
    * @throws IOException 
    */ 
public void createInFile(String filePath) throws IOException { 
-  this.inFile = new ExifInterface(filePath); 
+  if (filePath != null) { 
+   this.inFile = new ExifInterface(filePath); 
+  } else { 
+   throw new IOException("null pointer passed to createInFile"); 
+  } 
} 

/** 
@@ -63,7 +67,11 @@ public class ExifHelper { 
    * @throws IOException 
    */ 
public void createOutFile(String filePath) throws IOException { 
-  this.outFile = new ExifInterface(filePath); 
+  if (filePath != null) { 
+   this.outFile = new ExifInterface(filePath); 
+  } else { 
+   throw new IOException("null pointer passed to createOutFile"); 
+  } 
} 

/** 
diff --git a/src/android/FileHelper.java b/src/android/FileHelper.java 
index 59f890e..cd4b6ef 100644 
--- a/src/android/FileHelper.java 
+++ b/src/android/FileHelper.java 
@@ -33,6 +33,7 @@ import java.io.FileInputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.util.Locale; 
+import android.util.Log; 

public class FileHelper { 
private static final String LOG_TAG = "FileUtils"; 
@@ -54,9 +55,12 @@ public class FileHelper { 
realPath = FileHelper.getRealPathFromURI_BelowAPI11(cordova.getActivity(), uri); 

// SDK >= 11 && SDK < 19 
-  else if (Build.VERSION.SDK_INT < 19) 
+  else if (Build.VERSION.SDK_INT < 19) { 
realPath = FileHelper.getRealPathFromURI_API11to18(cordova.getActivity(), uri); 
- 
+   if (realPath == null) { 
+    realPath = getRealPathFallback(uri.toString(), cordova); 
+   } 
+  } 
// SDK > 19 (Android 4.4) 
else 
realPath = FileHelper.getRealPathFromURI_API19(cordova.getActivity(), uri); 
@@ -126,6 +130,47 @@ public class FileHelper { 
return result; 
} 

+ /** 
+  * Returns the real path of the given URI string. 
+  * If the given URI string represents a content:// URI, the real path is retrieved from the media store. 
+  * 
+  * @param uriString the URI string of the audio/image/video 
+  * @param cordova the current application context 
+  * @return the full path to the file 
+  */ 
+ @SuppressWarnings("deprecation") 
+ public static String getRealPathFallback(String uriString, CordovaInterface cordova) { 
+  String realPath = null; 
+  try { 
+   if (uriString.startsWith("content://")) { 
+    String[] proj = { _DATA }; 
+    Cursor cursor = cordova.getActivity().managedQuery(Uri.parse(uriString), proj, null, null, null); 
+    int column_index = cursor.getColumnIndexOrThrow(_DATA); 
+    cursor.moveToFirst(); 
+    realPath = cursor.getString(column_index); 
+    Log.d(LOG_TAG, "getRealPath managedQuery success uri " + uriString + " realpath " + realPath); 
+    if (realPath == null) { 
+     Log.e(LOG_TAG, "getRealPath Could get real path for URI string " + uriString); 
+    } 
+   } else if (uriString.startsWith("file://")) { 
+    realPath = uriString.substring(7); 
+    Log.d(LOG_TAG, "getRealPath file:// " + uriString + " realpath " + realPath); 
+    if (realPath.startsWith("/android_asset/")) { 
+     Log.e(LOG_TAG, "getRealPath Cannot get real path for URI string " + uriString + " because it is a file:///android_asset/ URI."); 
+     realPath = null; 
+    } 
+   } else { 
+    realPath = uriString; 
+   } 
+  } catch (Exception e) { 
+   Log.d(LOG_TAG, "getRealPath using uristring could not find real path, uriString: " + uriString + " message: " + e.getMessage()); 
+   realPath = uriString; 
+  } 
+ 
+  return realPath; 
+ } 
+ 
+ 
public static String getRealPathFromURI_BelowAPI11(Context context, Uri contentUri) { 
String[] proj = { MediaStore.Images.Media.DATA }; 
String result = null; 
Các vấn đề liên quan