gpt4 book ai didi

android - 从 webview INPUT 字段上传相机照片和文件选择器

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:16:36 27 4
gpt4 key购买 nike

我的应用程序是基于网络的,我需要上传来自 INPUT 野外营地的图片。我有两种情况,因为我不知道另一种方法,具体取决于页面,我根据其 URL 请愿书选择一个或另一个带有“boolean boolFileChoser”的页面:

一个。文件选择器

相机拍照。

我已经处理了文件选择器并且它完美地上传了文件, 问题出在相机上。一旦我尝试上传相机图片,它就会崩溃。据我所知,这是因为 URI。

a) 文件选择器:content://media/external/images/1234

b) 相机拍摄:file:///mnt/sdcard/Pic.jpg

我找不到改变它的方法。

查看更新

它现在因为在尝试上传“content://media/external/images/1234”时出现空指针异常而崩溃。 (仅适用于相机,不适用于文件选择器。)。此外,如果选择器/相机关闭(后退按钮),我将无法再次调用它。

情况 a) 和 b) 100% 工作,这里是工作代码,包括我如何知道是否调用了 fileChooser 或 camera:

@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%。如果你指出你是想要图片还是文件选择器

最佳答案

这就是我从 WebView 输入字段实现相机上传和文件选择器的方式:

下面是这个重要主题的代码。不相关的代码被删除。

public class MainActivity extends Activity {

private WebView webView;
private String urlStart = "http://www.example.com/mobile/";

//File choser parameters
private static final int FILECHOOSER_RESULTCODE = 2888;
private ValueCallback<Uri> mUploadMessage;

//Camera parameters
private Uri mCapturedImageURI = null;

@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

webView = (WebView) findViewById(R.id.webView);

webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadWithOverviewMode(true);

webView.getSettings().setAllowFileAccess(true);

webView.loadUrl(urlStart);

webView.setWebChromeClient(new WebChromeClient() {
// openFileChooser for Android 3.0+
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {

mUploadMessage = uploadMsg;

try{
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File externalDataDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DCIM);
File cameraDataDir = new File(externalDataDir.getAbsolutePath() +
File.separator + "browser-photos");
cameraDataDir.mkdirs();
String mCameraFilePath = cameraDataDir.getAbsolutePath() + File.separator +
System.currentTimeMillis() + ".jpg";
mCapturedImageURI = Uri.fromFile(new File(mCameraFilePath));

cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);

Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");

Intent chooserIntent = Intent.createChooser(i, "Image Chooser");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Parcelable[] { cameraIntent });

startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE);
}
catch(Exception e){
Toast.makeText(getBaseContext(), "Camera Exception:"+e, Toast.LENGTH_LONG).show();
}
}

// For Android < 3.0
@SuppressWarnings("unused")
public void openFileChooser(ValueCallback<Uri> uploadMsg ) {
openFileChooser(uploadMsg, "");
}

// For Android > 4.1.1
@SuppressWarnings("unused")
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture){
openFileChooser(uploadMsg, acceptType);
}



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", "www.example.com: " + message);
}
});
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
// TODO Auto-generated method stub
if(requestCode==FILECHOOSER_RESULTCODE)
{

if (null == this.mUploadMessage) {
return;
}

Uri result=null;

try{
if (resultCode != RESULT_OK) {

result = null;

} else {

// retrieve from the private variable if the intent is null
result = intent == null ? mCapturedImageURI : intent.getData();
}
}
catch(Exception e)
{
Toast.makeText(getApplicationContext(), "activity :"+e, Toast.LENGTH_LONG).show();
}

mUploadMessage.onReceiveValue(result);
mUploadMessage = null;

}
}

希望对大家有帮助:)

关于android - 从 webview INPUT 字段上传相机照片和文件选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13284903/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com