问题描述
我的应用程序是基于Web的,我需要从输入字段阵营上传图片。我有两种情况,当我不知道另一种方式来做到这一点取决于我选择一种或另一种以布尔boolFileChoser,这取决于它的URL申请页面:
My app is webbased and I need to upload pictures from an INPUT field camp. I've two situations and as i don't know another way to do it depending the page I'm choosing one or another with "boolean boolFileChoser" depending its URL petition:
一个。文件选择器
乙。相机拍出的照片。
我已经处理了文件选择器,并将其上传文件完美,
It now crashes because a nullpointerexception while trying to upload the "content://media/external/images/1234". (only with camera, not file chooser. ).Also if the chooser/camera is closed (back button), i'm unable to call it again.
情况a)和b)100%的工作,这里的工作code,包括我怎么知道,如果文件选择器或相机被称为:
Case a) and b) 100% working, here's the working code, including how I know if fileChooser or camera are called:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (resultCode != RESULT_OK) {
/** fixed code **/
//To be able to use the filechooser again in case of error
mUploadMessage.onReceiveValue(null);
/** fixed code **/
return;
}
if (mUploadMessage==null) {
Log.d("androidruntime","no mUploadMessage");
return;
}
if (requestCode == FILECHOOSER_RESULTCODE) {
Uri selectedImage= intent == null || resultCode != RESULT_OK ? null : intent.getData();
Log.d("androidruntime","url: "+selectedImage.toString());
}else if (requestCode == CAMERAREQUEST_RESULTCODE) {
if(mCapturedImageURI==null){
Log.d("androidruntime","no mCapturedImageURI");
return;
}
/** fixed code **/
getContentResolver().notifyChange(mCapturedImageURI, null);
ContentResolver cr = getContentResolver();
Uri uriContent= Uri.parse(MediaStore.Images.Media.insertImage(getContentResolver(), photo.getAbsolutePath(), null, null));
photo = null;
/** fixed code **/
}
mUploadMessage.onReceiveValue(selectedImage);
mUploadMessage = null;
}
private static final int FILECHOOSER_RESULTCODE = 2888;
private static final int CAMERAREQUEST_RESULTCODE = 1888;
private ValueCallback<Uri> mUploadMessage;
private Uri mCapturedImageURI = null;
protected class AwesomeWebChromeClient extends WebChromeClient{
// Per Android 3.0+
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType){
/**updated, out of the IF **/
mUploadMessage = uploadMsg;
/**updated, out of the IF **/
if(boolFileChooser){ //Take picture from filechooser
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
MainActivity.this.startActivityForResult( Intent.createChooser( i, "Escoger Archivo" ), MainActivity.FILECHOOSER_RESULTCODE );
} else { //Take photo and upload picture
Intent cameraIntent = new Intent("android.media.action.IMAGE_CAPTURE");
File photo = new File(Environment.getExternalStorageDirectory(), "Pic.jpg");
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photo));
mCapturedImageURI = Uri.fromFile(photo);
startActivityForResult(cameraIntent, MainActivity.CAMERA_REQUEST);
}
}
// Per Android < 3.0
public void openFileChooser(ValueCallback<Uri> uploadMsg){
openFileChooser(uploadMsg, "");
}
//Altre
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
openFileChooser(uploadMsg, "");
}
/** Added code to clarify chooser. **/
//The webPage has 2 filechoosers and will send a console message informing what action to perform, taking a photo or updating the file
public boolean onConsoleMessage(ConsoleMessage cm) {
onConsoleMessage(cm.message(), cm.lineNumber(), cm.sourceId());
return true;
}
public void onConsoleMessage(String message, int lineNumber, String sourceID) {
Log.d("androidruntime", "Per cònsola: " + cm.message());
if(message.endsWith("foto")){ boolFileChooser= true; }
else if(message.endsWith("pujada")){ boolFileChooser= false; }
}
/** Added code to clarify chooser. **/
}
更新1
我能得到内容://媒体/外部/图像/ XXXURI格式,但应用程序仍然在试图上传通过URI崩溃mUploadMessage.onReceiveValue(selectedImage);。现在,我得到一个NullPointerException异常。
I could get the "content://media/external/images/xxx" uri format, but the app still crashes while trying to upload the uri via "mUploadMessage.onReceiveValue(selectedImage);". Now I'm getting a nullpointerexception.
更新2
固定和工作。
我已经有'ValueCallback uploadMsg在局部变量只在文件选择器的情况下,所以它永诺扔我一个例外,当我试图上传照片文件,因为它为空。有一次,我拿出从if-else语句,所有的工作。在previous更新是处理文件上传的最简单的方法。
I've had the 'ValueCallback uploadMsg' in local variable only in file-chooser case, so it allways throwed me an exception when i tried to upload a photo file because it was null.Once i took out from if-else statement, all worked.The previous update was the easiest method to deal with the file upload.
我已经添加了mUploadMessage.onReceiveValue(空);如果相机/文件选择器意图被取消(你必须对付它在你的网页),如果没有,你将无法启动INPUT场(意向)了。
I've already added a 'mUploadMessage.onReceiveValue(null);' if the Camera/filechooser intent is cancelled (you must deal with it in your webpage), if not, you won't be able to launch the INPUT field (Intent) again.
更新3
加内AwesomeChromeClient的code中的一部分,以区分选项,拍照或选择一个文件..做它自己的路,并通过信访补充说,我敢肯定有是很多其他的有效的方法来做到这一点,
Added the part of the code inside AwesomeChromeClient to discriminate the option, take a photo or choose a file.. its MY way of doing it and added by petition, i'm sure there're a lot of other valid ways to do it,
在code是100%的功能了。如果您指明是否想要看的图像或文件选择
The code is 100% functionally now. If you indicate if you want picture or file-chooser
推荐答案
解决。在我的问题,还有功能code的情况下,任何人都需要它。
Solved. Inside my question, there's the functional code in case anyone needs it.
下面是这些问题的解决方案:
Here's the solution of the issues:
-
无法打开相机/文件选择器,如果我previously开通和取消的:
Couldn't open camera/filechooser if I previously opened and cancelled:
//inside onActivityResult
if (resultCode != RESULT_OK) {
mUploadMessage.onReceiveValue(null);
return;
}
获取内容://媒体/外部/图像/ XXXURI格式,上传通过URImUploadMessage.onReceiveValue(selectedImage);,避免NullPointerException异常
Get the "content://media/external/images/xxx" uri format, to upload the uri via "mUploadMessage.onReceiveValue(selectedImage);", avoiding a nullpointerexception
//inside OnActivityResult
getContentResolver().notifyChange(mCapturedImageURI, null);
ContentResolver cr = getContentResolver();
Uri uriContent = Uri.parse(android.provider.MediaStore.Images.Media.insertImage(getContentResolver(), photo.getAbsolutePath(), null, null));
这篇关于上传相机照片和文件选择器从web视图输入字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!