【发布时间】:2012-10-28 09:43:33
【问题描述】:
我的应用是基于网络的,我需要上传来自 INPUT 野外营地的图片。我有两种情况,因为我不知道另一种方法来做到这一点,这取决于我选择一个或另一个带有“boolean boolFileChoser”的页面,具体取决于它的 URL 请愿:
一个。文件选择器
b.相机拍照。
我已经处理了文件选择器,它完美地上传了文件,问题出在相机上。一旦我尝试上传相机图片,它就会崩溃。
据我所知,这是因为 URI。
a) 文件选择器:content://media/external/images/1234
b) 相机拍摄:file:///mnt/sdcard/Pic.jpg
我没有办法改变它。
查看更新
它现在崩溃,因为在尝试上传“content://media/external/images/1234”时出现空指针异常。 (仅适用于相机,不适用于文件选择器。)。 此外,如果选择器/相机已关闭(后退按钮),我将无法再次调用它。
案例 a) 和 b) 100% 工作,这是工作代码,包括我如何知道是否调用了 fileChooser 或相机:
@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
我可以获得“content://media/external/images/xxx” uri 格式,但应用程序在尝试通过“mUploadMessage.onReceiveValue(selectedImage);”上传 uri 时仍然崩溃。现在我得到一个空指针异常。
更新 2
已修复并正常工作。
我只在文件选择器的情况下在局部变量中有'ValueCallback uploadMsg',所以当我尝试上传照片文件时它总是抛出一个异常,因为它是空的。 一旦我从 if-else 语句中取出,一切正常。 之前的更新是处理文件上传最简单的方法。
我已经添加了一个 'mUploadMessage.onReceiveValue(null);'如果Camera/filechooser intent被取消(你必须在你的网页中处理它),如果没有,你将无法再次启动INPUT字段(Intent)。
更新 3
在 AwesomeChromeClient 中添加了部分代码以区分选项、拍照或选择文件。这是我的做法并通过请愿书添加,我相信还有很多其他有效的方法可以做吧,
代码现在功能 100%。如果你指明你想要图片还是文件选择器
【问题讨论】:
-
已修复所有错误并使用工作代码进行更新。
-
代码看起来不错,对我很有用,我还没试过,我想弄清楚变量“boolFileChooser”的值在哪里设置?还发现了一个错字:我认为“MainActivity.CAMERA_REQUEST”应该是“MainActivity.CAMERAREQUEST_RESULTCODE”
-
我现在已经设法让我的代码工作了,非常感谢您的帮助。在网上尝试了许多其他解决方案,但这个是最好的。干杯!
-
可以上传完整代码吗?谢谢。
-
user280109 我记得我也尝试过(许多不同的来源和代码尝试),我不记得我为什么放那个,但是“它有效”:DI 无法发布完整的代码(应用程序和 oncreate 中的标准 webview 的特定代码),但我添加了“boolFileChooser”部分..这是使它工作的缺失部分,所以它应该足够了..当我写这个时,它还没有实现。 (我一直在外面,很抱歉没有早点回答你)。
标签: android android-camera webviewclient filechooser