오류가 하나도 없는 웹뷰앱 + 파일권한
*** 완성본은 곧 스토어에 올라옵니다 ***
모르는건 댓글 남겨주세용
기본 설명 : 자바입니다. / 프로젝트 생성할 때 Use legacy android.support libraries 선택해주셔야해요.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp" />
</android.support.constraint.ConstraintLayout>
-
MainActivity.Java
패키지 명 밑에다가 해주시고, 빨간색으로 에러뜨는 부분은 Alt Enter 누르셔서 import 해주세요.
사이트 주소 부분에는 본인 사이트 주소 적으시면 됩니다. http or https 까지요
public class MainActivity extends AppCompatActivity {
public static final String TAG = "사이트 이름";
public static final int IMAGE_SELECTOR_REQ = 999;
public static final int KAKAO_SHARE_REQ = 998;
public static final String SERVER_IP = "사이트주소";
public static final String SERVER_HOST = "사이트주소";
private WebView myWebView;
private ValueCallback mFilePathCallback;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// HashGetter.getHashKey(getApplicationContext());
myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebChromeClient(new MyWebChromeClient());
setGPULayerType(myWebView);
myWebView.setWebViewClient(new WebViewClient());
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setRenderPriority(WebSettings.RenderPriority.HIGH);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.setPluginState(WebSettings.PluginState.ON);
myWebView.loadUrl(SERVER_IP);
}
private void setGPULayerType(WebView webView) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
} else {
webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK){
switch (requestCode) {
case IMAGE_SELECTOR_REQ:
if (data.getClipData() != null) {
int count = data.getClipData().getItemCount();
Uri[] uris = new Uri[count];
for (int i = 0; i < count; i++)
uris[i] = data.getClipData().getItemAt(i).getUri();
mFilePathCallback.onReceiveValue(uris);
} else if (data.getData() != null) {
mFilePathCallback.onReceiveValue(new Uri[]{data.getData()});
}
break;
case KAKAO_SHARE_REQ:
Log.d(TAG, "onActivityResult: Kakao Share");
myWebView.getOriginalUrl();
break;
}
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
myWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
private class MyWebChromeClient extends WebChromeClient {
@Override
public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT).show();
result.confirm();
return true;
}
@Override
public boolean onShowFileChooser(WebView webView, ValueCallback filePathCallback,
WebChromeClient.FileChooserParams fileChooserParams) {
mFilePathCallback = filePathCallback;
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
startActivityForResult(Intent.createChooser(intent,"Select Picture"), IMAGE_SELECTOR_REQ);
return true;
}
}
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("사이트주소")) {
// This is my website, so do not override; let my WebView load the page
return false;
}
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
}
AndroidManifest.xml
다른 코드 삭제하지 마시고 package 밑에다가 이 코드를 넣어주세요.
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
만약 사이트 주소가 http라면
android:supportsRtl="true" 밑에다가
android:usesCleartextTraffic="true" 를 넣어주세요.
이 코드 넣어주세요.
끝입니다.
게시판 목록
자유게시판
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 199717 | 2일 전 | 4 | ||
| 199716 |
느긋한카키쿠키
|
2일 전 | 2 | |
| 199715 |
현대적인무질서한까마귀
|
2일 전 | 5 | |
| 199714 | 2일 전 | 9 | ||
| 199713 | 3일 전 | 17 | ||
| 199712 | 2주 전 | 247 | ||
| 199711 |
안졸리니졸리니
|
2주 전 | 122 | |
| 199710 |
|
2주 전 | 137 | |
| 199709 |
|
2주 전 | 76 | |
| 199708 | 3주 전 | 90 | ||
| 199707 | 1개월 전 | 223 | ||
| 199706 | 1개월 전 | 26 | ||
| 199705 | 1개월 전 | 15 | ||
| 199704 | 1개월 전 | 32 | ||
| 199703 | 1개월 전 | 41 | ||
| 199702 | 1개월 전 | 80 | ||
| 199701 | 1개월 전 | 95 | ||
| 199700 | 1개월 전 | 69 | ||
| 199699 | 1개월 전 | 71 | ||
| 199698 | 1개월 전 | 117 | ||
| 199697 | 1개월 전 | 83 | ||
| 199696 |
|
1개월 전 | 230 | |
| 199695 | 1개월 전 | 71 | ||
| 199694 | 1개월 전 | 98 | ||
| 199693 | 1개월 전 | 166 | ||
| 199692 | 1개월 전 | 167 | ||
| 199691 |
|
1개월 전 | 140 | |
| 199690 | 1개월 전 | 235 | ||
| 199689 | 1개월 전 | 145 | ||
| 199688 | 1개월 전 | 172 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기