0


安卓webview不支持input type=file问题(转载测试可用)

参考博客地址

转载原文博客地址

public class MainActivity extends AppCompatActivity {

private ValueCallback mUploadMessage;// 表单的数据信息

private ValueCallback<Uri[]> mUploadCallbackAboveL;

private final static int FILECHOOSER_RESULTCODE = 1;// 表单的结果回调

private Uri imageUri;

private WebView mWebView;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main2);

mWebView= (WebView) findViewById(R.id.webview);

mWebView.loadUrl(“网页地址”);

mWebView.getSettings().setJavaScriptEnabled(true);

mWebView.setWebViewClient(new WebViewClient() {

@Override

public boolean shouldOverrideUrlLoading(WebView view, String url) {

return super.shouldOverrideUrlLoading(view, url);

}

});

mWebView.setWebChromeClient(new WebChromeClient() {

@Override

public boolean onShowFileChooser(WebView webView,

ValueCallback<Uri[]> filePathCallback,

FileChooserParams fileChooserParams) {

mUploadCallbackAboveL = filePathCallback;

take();

return true;

}

});

}

@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)

@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

super.onActivityResult(requestCode, resultCode, data);

if (requestCode == FILECHOOSER_RESULTCODE) {

if (null == mUploadMessage && null == mUploadCallbackAboveL) return;

Uri result = data == null || resultCode != RESULT_OK ? null : data.getData();

if (mUploadCallbackAboveL != null) {

onActivityResultAboveL(requestCode, resultCode, data);

} else if (mUploadMessage != null) {

if (result != null) {

String path = getPath(getApplicationContext(),

result);

Uri uri = Uri.fromFile(new File(path));

mUploadMessage.onReceiveValue(uri);

} else {

mUploadMessage.onReceiveValue(imageUri);

}

mUploadMessage = null;

}

}

}

@SuppressWarnings(“null”)

@android.support.annotation.RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)

private void onActivityResultAboveL(int requestCode, int resultCode, Intent data) {

if (requestCode != FILECHOOSER_RESULTCODE

|| mUploadCallbackAboveL == null) {

return;

}

Uri[] results = null;

if (resultCode == Activity.RESULT_OK) {

if (data == null) {

results = new Uri[]{imageUri};

} else {

String dataString = data.getDataString();

ClipData clipData = data.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)};

}

}

if (results != null) {

mUploadCallbackAboveL.onReceiveValue(results);

mUploadCallbackAboveL = null;

} else {

results = new Uri[]{imageUri};

mUploadCallbackAboveL.onReceiveValue(results);

mUploadCallbackAboveL = null;

}

return;

}

private void take() {

File imageStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), “MyApp”);

if (!imageStorageDir.exists()) {

imageStorageDir.mkdirs();

}

File file = new File(imageStorageDir + File.separator + “IMG_” + String.valueOf(System.currentTimeMillis()) + “.jpg”);

imageUri = Uri.fromFile(file);

final List cameraIntents = new ArrayList();

final Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

final PackageManager packageManager = getPackageManager();

final List listCam = packageManager.queryIntentActivities(captureIntent, 0);

for (ResolveInfo res : listCam) {

final String packageName = res.activityInfo.packageName;

final Intent i = new Intent(captureIntent);

i.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));

i.setPackage(packageName);

i.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);

cameraIntents.add(i);

}

Intent i = new Intent(Intent.ACTION_GET_CONTENT);

i.addCategory(Intent.CATEGORY_OPENABLE);

i.setType(“image/*”);

Intent chooserIntent = Intent.createChooser(i, “Image Chooser”);

MainActivity.this.startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE);

}

@SuppressLint(“NewApi”)

@TargetApi(Build.VERSION_CODES.KITKAT)

public static String getPath(final Context context, final Uri uri) {

final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;

// DocumentProvider

if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {

// ExternalStorageProvider

if (isExternalStorageDocument(uri)) {

final String docId = DocumentsContract.getDocumentId(uri);

final String[] split = docId.split(“:”);

final String type = split[0];

if (“primary”.equalsIgnoreCase(type)) {

return Environment.getExternalStorageDirectory() + “/” + split[1];

}

} else if (isDownloadsDocument(uri)) {

final String id = DocumentsContract.getDocumentId(uri);

final Uri contentUri = ContentUris.withAppendedId(Uri.parse(“content://downloads/public_downloads”), Long.valueOf(id));

return getDataColumn(context, contentUri, null, null);

} else if (isMediaDocument(uri)) {

final String docId = DocumentsContract.getDocumentId(uri);

final String[] split = docId.split(“:”);

final String type = split[0];

Uri contentUri = null;

if (“image”.equals(type)) {

contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;

} else if (“video”.equals(type)) {

contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;

} else if (“audio”.equals(type)) {

contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;

}

final String selection = “_id=?”;

final String[] selectionArgs = new String[]{split[1]};

return getDataColumn(context, contentUri, selection, selectionArgs);

}

} else if (“content”.equalsIgnoreCase(uri.getScheme())) {

return getDataColumn(context, uri, null, null);

} else if (“file”.equalsIgnoreCase(uri.getScheme())) {

return uri.getPath();

总结

大厂面试问深度,小厂面试问广度,如果有同学想进大厂深造一定要有一个方向精通的惊艳到面试官,还要平时遇到问题后思考一下问题的本质,找方法解决是一个方面,看到问题本质是另一个方面。还有大家一定要有目标,我在很久之前就想着以后一定要去大厂,然后默默努力,每天看一些大佬们的文章,总是觉得只有再学深入一点才有机会,所以才有恒心一直学下去。


本文转载自: https://blog.csdn.net/2401_85123379/article/details/139740972
版权归原作者 2401_85123379 所有, 如有侵权,请联系我们删除。

“安卓webview不支持input type=file问题(转载测试可用)”的评论:

还没有评论