我开发了一个 JSF 应用程序,允许用户拍摄他们的自拍并将其发布到服务器以获取他们在系统中的出席情况。所有的东西都运行良好,但突然之间没有任何变化,应用程序开始出现问题。在调试时,我发现onActivityResult
's中的代码resultCode
为 0,但相同的例程在 Chrome 中运行良好。
代码onActivityResult
如下:-
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent){
try {
// Log.i(TAG, "onActivityResult: " + " " + requestCode + ", " + resultCode + ", " + intent.toString());
Toast.makeText(getApplicationContext(), "onActivityResult Result ! Line No. 1 requestCode : " + requestCode + ", " + resultCode + ", " + Activity.RESULT_OK, Toast.LENGTH_LONG).show();
super.onActivityResult(requestCode, resultCode, intent);
if(Build.VERSION.SDK_INT >= 21){
Toast.makeText(getApplicationContext(), "onActivityResult Result ! Line No. 2", Toast.LENGTH_LONG).show();
Uri[] results = null;
/*-- if file request cancelled; exited camera. we need to send null value to make future attempts workable --*/
if (resultCode == Activity.RESULT_CANCELED) {
Toast.makeText(getApplicationContext(), "onActivityResult Result ! Line No. 3", Toast.LENGTH_LONG).show();
file_path.onReceiveValue(null);
return;
}
/*-- continue if response is positive --*/
if(resultCode== Activity.RESULT_OK){
Toast.makeText(getApplicationContext(), "onActivityResult Result ! Line No. 4", Toast.LENGTH_LONG).show();
if(null == file_path){
return;
}
ClipData clipData;
String stringData;
try {
clipData = intent.getClipData();
stringData = intent.getDataString();
}catch (Exception e){
clipData = null;
stringData = null;
}
if (clipData == null && stringData == null && cam_file_data != null) {
results = new Uri[]{Uri.parse(cam_file_data)};
}else{
if (clipData != null) { // checking if multiple files selected or not
final int numSelectedFiles = clipData.getItemCount();
results = new Uri[numSelectedFiles];
for (int i = 0; i < clipData.getItemCount(); i++) {
results[i] = clipData.getItemAt(i).getUri();
}
} else {
try {
Bitmap cam_photo = (Bitmap) intent.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
cam_photo.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
stringData = MediaStore.Images.Media.insertImage(this.getContentResolver(), cam_photo, null, null);
}catch (Exception ignored){}
/* checking extra data
Bundle bundle = intent.getExtras();
if (bundle != null) {
for (String key : bundle.keySet()) {
Log.w("ExtraData", key + " : " + (bundle.get(key) != null ? bundle.get(key) : "NULL"));
}
}*/
// Toast.makeText(getApplicationContext(), "On Activity Result ! " + stringData.toString(), Toast.LENGTH_LONG).show();
results = new Uri[]{Uri.parse(stringData)};
Toast.makeText(getApplicationContext(), "onActivityResult Result ! " + stringData + ", " + results.toString() + ", " + results.length, Toast.LENGTH_LONG).show();
}
}
}
file_path.onReceiveValue(results);
file_path = null;
}else{
if(requestCode == file_req_code){
if(null == file_data) return;
Uri result = intent == null || resultCode != RESULT_OK ? null : intent.getData();
file_data.onReceiveValue(result);
file_data = null;
}
}
}catch (Exception ex){
Toast.makeText(getApplicationContext(), "onActivityResult Error ! " + ex.getMessage(), Toast.LENGTH_LONG).show();
}
}
以下是上的代码.setWebChomeClient
webView.setWebChromeClient(new WebChromeClient() {
/*-- handling input[type="file"] requests for android API 21+ --*/
public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
if(file_permission() && Build.VERSION.SDK_INT >= 21) {
// Toast.makeText(getApplicationContext(), "Downloading-File-Error ! " + filePathCallback.toString(), Toast.LENGTH_LONG).show();
file_path = filePathCallback;
Intent takePictureIntent = null;
Intent takeVideoIntent = null;
boolean includeVideo = false;
boolean includePhoto = true;
/*-- checking the accept parameter to determine which intent(s) to include --*/
paramCheck:
for (String acceptTypes : fileChooserParams.getAcceptTypes()) {
String[] splitTypes = acceptTypes.split(", ?+"); // although it's an array, it still seems to be the whole value; split it out into chunks so that we can detect multiple values
for (String acceptType : splitTypes) {
switch (acceptType) {
case "*/*":
includePhoto = true;
includeVideo = true;
break paramCheck;
case "image/*":
includePhoto = true;
break;
case "video/*":
includeVideo = true;
break;
}
}
}
if (fileChooserParams.getAcceptTypes().length == 0) { //no `accept` parameter was specified, allow both photo and video
includePhoto = true;
includeVideo = true;
}
if (includePhoto) {
takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(MainActivity.this.getPackageManager()) != null) {
File photoFile = null;
try {
photoFile = create_image();
takePictureIntent.putExtra("PhotoPath", cam_file_data);
} catch (IOException ex) {
Log.e(TAG, "Image file creation failed", ex);
}
if (photoFile != null) {
cam_file_data = "file:" + photoFile.getAbsolutePath();
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
} else {
cam_file_data = null;
takePictureIntent = null;
}
}
}
if (includeVideo) {
takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
if (takeVideoIntent.resolveActivity(MainActivity.this.getPackageManager()) != null) {
File videoFile = null;
try {
videoFile = create_video();
} catch (IOException ex) {
Log.e(TAG, "Video file creation failed", ex);
}
if (videoFile != null) {
cam_file_data = "file:" + videoFile.getAbsolutePath();
takeVideoIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(videoFile));
} else {
cam_file_data = null;
takeVideoIntent = null;
}
}
}
Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
contentSelectionIntent.setType(file_type);
if (multiple_files) {
contentSelectionIntent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
}
Intent[] intentArray;
if (takePictureIntent != null && takeVideoIntent != null) {
intentArray = new Intent[]{takePictureIntent, takeVideoIntent};
} else if (takePictureIntent != null) {
intentArray = new Intent[]{takePictureIntent};
} else if (takeVideoIntent != null) {
intentArray = new Intent[]{takeVideoIntent};
} else {
intentArray = new Intent[0];
}
Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);
chooserIntent.putExtra(Intent.EXTRA_TITLE, "File chooser");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
startActivityForResult(chooserIntent, file_req_code);
return true;
} else {
return false;
}
}
});
webView.loadUrl(webview_url);
}
现在在 Android WebView 应用程序中,当用户走到相机前拍照时,照片已拍摄但未保存在图库中,但在 Chrome 中可以使用相同的功能。
请提出一些解决方案。