Skip to content

Commit

Permalink
上传文件
Browse files Browse the repository at this point in the history
  • Loading branch information
harry committed Dec 24, 2019
1 parent f009f4c commit ee97f9b
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 37 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
/build
/captures
.externalNativeBuild
.idea
9 changes: 3 additions & 6 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 0 additions & 29 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

<!--需要允许网络-->
<uses-permission android:name="android.permission.INTERNET" />
<!--上传文件允许-->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<!--android9开始usesCleartextTraffic默认为false, 需要设置true-->
<application
Expand Down
38 changes: 38 additions & 0 deletions app/src/main/java/com/example/harry/myapplication/MuduRoom.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.example.harry.myapplication;

import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.net.Uri;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
Expand All @@ -10,6 +13,7 @@
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
Expand All @@ -19,6 +23,11 @@ public class MuduRoom extends AppCompatActivity {

private final String TAG = "MuduRoom";

// webview上传文件临时变量存储
public ValueCallback<Uri[]> filePathCallback;
public static final int FILECHOOSER_RESULTCODE_FOR_ANDROID_5 = 5174;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand Down Expand Up @@ -87,6 +96,24 @@ public void onHideCustomView() {
// 设置屏幕为竖屏
curActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}

@Override
public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
// android5.0+ webview上传文件实现, 参考:https://juejin.im/post/585a4d0b128fe1006b906f16
if (((MuduRoom) curActivity).filePathCallback != null) {
((MuduRoom) curActivity).filePathCallback.onReceiveValue(null);
((MuduRoom) curActivity).filePathCallback = null;
}
((MuduRoom) curActivity).filePathCallback = filePathCallback;
Intent intent = fileChooserParams.createIntent();
try {
((MuduRoom) curActivity).startActivityForResult(intent, FILECHOOSER_RESULTCODE_FOR_ANDROID_5);
} catch (ActivityNotFoundException e) {
((MuduRoom) curActivity).filePathCallback = null;
return false;
}
return true;
}
});

// 设置WebViewClient,防止使用浏览器打开
Expand Down Expand Up @@ -129,4 +156,15 @@ public void showTitleBar () {
// 取消屏幕常亮
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}


@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
// 接收文件上传消息
if (requestCode == FILECHOOSER_RESULTCODE_FOR_ANDROID_5) {
if (filePathCallback == null) return;
filePathCallback.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, data));
filePathCallback = null;
}
}
}
2 changes: 1 addition & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<resources>
<string name="app_name">视频webview嵌入测试</string>
<string name="default_url">http://mudu.tv/watch/2758917</string>
<string name="default_url">http://mudu.tv/watch/4301650</string>
<string name="button_text">点击进入</string>
<string name="input_hint">视频地址</string>
</resources>

0 comments on commit ee97f9b

Please sign in to comment.