gpt4 book ai didi

android - 图像是从画廊上传的,而不是通过 webView 从相机上传的

转载 作者:行者123 更新时间:2023-11-29 23:27:24 25 4
gpt4 key购买 nike

我正在尝试使用 webview 上传图片。一切都按预期正常工作,但当从相机中单击照片时,只有图像不会上传。请帮我找到这个。我得到了一些答案,但没有帮助或没有回答 like this one is not answeredthis doesn't help me this is directly opening gallery not working for camera this one didn't help me

这是我的 Activity 代码

public class HomeActivity extends Activity {

private WebView webView;
private ValueCallback<Uri> mUploadMessage;
private ValueCallback<Uri[]> mUploadMessages;
private Uri mCapturedImageURI = null;
private static final int MY_CAMERA_REQUEST_CODE = 100;
private static final int FILECHOOSER_RESULTCODE = 1;

@RequiresApi(api = Build.VERSION_CODES.M)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);

webView = findViewById(R.id.webView);

webView.setWebViewClient(new WebViewClient());

WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setPluginState(WebSettings.PluginState.ON);
webView.getSettings().setAppCacheEnabled(false);
webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setBuiltInZoomControls(false);
webView.getSettings().setSupportZoom(false);
webView.getSettings().setDefaultZoom(WebSettings.ZoomDensity.FAR);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setDatabaseEnabled(true);
webView.getSettings().setDatabasePath("/data/data/" + webView.getContext().getPackageName() + "/databases/");

webView.loadUrl("url");

webView.setWebChromeClient(new WebChromeClient() {

// For api level bellow 24
public boolean shouldOverrideUrlLoading(WebView view, String url) {

if (url.startsWith("http")) {
// Return false means, web view will handle the link
return false;
}

return false;
}


// From api level 24
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
/*Toast.makeText(mcontext, "New Method",Toast.LENGTH_SHORT).show();*/

// Get the tel: url
String url = request.getUrl().toString();

if (url.startsWith("http")) {
// Return false means, web view will handle the link
return false;
}

return false;
}

// openFileChooser for Android 3.0+

/* public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
mUploadMessage = uploadMsg;
openImageChooser();
}*/

// For Lollipop 5.0+ Devices

public boolean onShowFileChooser(WebView mWebView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams) {
mUploadMessages = filePathCallback;
openImageChooser();
return true;
}

// openFileChooser for Android < 3.0

/* public void openFileChooser(ValueCallback<Uri> uploadMsg) {
openFileChooser(uploadMsg, "");
}

//openFileChooser for other Android versions

public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
openFileChooser(uploadMsg, acceptType);
}*/
});

}

private void openImageChooser() {
try {

File imageStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "FolderName");

if (!imageStorageDir.exists()) {
imageStorageDir.mkdirs();
}
File file = new File(imageStorageDir + File.separator + "IMG_" + String.valueOf(System.currentTimeMillis()) + ".jpg");
mCapturedImageURI = Uri.fromFile(file);

final Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
captureIntent.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[]{captureIntent});

startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE);
} catch (Exception e) {
e.printStackTrace();
}
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);

if (requestCode == FILECHOOSER_RESULTCODE) {

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

/* Uri result;
if (requestCode != RESULT_OK){
result = null;
}else {
result = intent == null ? this.mCapturedImageURI : intent.getData();
}
this.mUploadMessage.onReceiveValue(result);
this.mUploadMessage = null;*/
if (null != mUploadMessage) {
handleUploadMessage(requestCode, resultCode, intent);

} else if (mUploadMessages != null) {
handleUploadMessages(requestCode, resultCode, intent);
}
}
}

private void handleUploadMessage(int requestCode, int resultCode, Intent intent) {
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) {
e.printStackTrace();
}
mUploadMessage.onReceiveValue(result);
mUploadMessage = null;
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void handleUploadMessages(int requestCode, int resultCode, Intent intent) {
Uri[] results = null;
try {
if (resultCode != RESULT_OK) {
results = null;
} else {
if (intent != null) {
String dataString = intent.getDataString();
ClipData clipData = intent.getClipData();
if (clipData != null) {
results = new Uri[clipData.getItemCount()];
for (int i = 0; i < clipData.getItemCount(); i++) {
ClipData.Item item = clipData.getItemAt(i);
results[i] = item.getUri();
}
}
if (dataString != null) {
results = new Uri[]{Uri.parse(dataString)};
}
} else {
results = new Uri[]{mCapturedImageURI};
}
}
} catch (Exception e) {
e.printStackTrace();
}
mUploadMessages.onReceiveValue(results);
mUploadMessages = null;
}

}

最佳答案

您的代码很好,您只需要务实地添加存储和相机权限。它会像魅力一样工作。

关于android - 图像是从画廊上传的,而不是通过 webView 从相机上传的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53333146/

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