android webview html file 사용
Posted by Albert 3111Day 13Hour 20Min 19Sec ago [2016-10-13]
전역변수
private ValueCallback<Uri> filePathCallbackNormal;
private ValueCallback<Uri[]> filePathCallbackLollipop;
private Uri mCapturedImageURI;
OnCreate WebChromeSetting
// web view 셋팅
webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
mWebViewInterface = new WebViewInterface(MainActivity.this, webView); //JavascriptInterface 객체화
webView.addJavascriptInterface(mWebViewInterface, "Android"); //웹뷰에 JavascriptInterface를 연결
// 현재 웹뷰에서 처리하도록...
webView.setWebViewClient(new EsavingWebViewClient());
// 크롬 클라이언트 생성
webView.setWebChromeClient(new WebChromeClient() {
// For Android < 3.0
public void openFileChooser(ValueCallback<Uri> uploadMsg) {
openFileChooser(uploadMsg, "");
}
// For Android 3.0+
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
filePathCallbackNormal = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
startActivityForResult(Intent.createChooser(i, "File Chooser"), ESavingConstants.FILECHOOSER_NORMAL_REQ_CODE);
}
// For Android 4.1+
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
openFileChooser(uploadMsg, acceptType);
}
// For Android 5.0+
public boolean onShowFileChooser(
WebView webView, ValueCallback<Uri[]> filePathCallback,
WebChromeClient.FileChooserParams fileChooserParams) {
if (filePathCallbackLollipop != null) {
// filePathCallbackLollipop.onReceiveValue(null);
filePathCallbackLollipop = null;
}
filePathCallbackLollipop = filePathCallback;
// Create AndroidExampleFolder at sdcard
File imageStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "AndroidExampleFolder");
if (!imageStorageDir.exists()) {
// Create AndroidExampleFolder at sdcard
imageStorageDir.mkdirs();
}
// Create camera captured image file path and name
File file = new File(imageStorageDir + File.separator + "IMG_" + String.valueOf(System.currentTimeMillis()) + ".jpg");
mCapturedImageURI = Uri.fromFile(file);
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/*");
// Create file chooser intent
Intent chooserIntent = Intent.createChooser(i, "Image Chooser");
// Set camera intent to file chooser
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Parcelable[]{captureIntent});
// On select image call onActivityResult method of activity
startActivityForResult(chooserIntent, ESavingConstants.FILECHOOSER_LOLLIPOP_REQ_CODE);
return true;
}
});
onActivityResult 설정
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == ESavingConstants.FILECHOOSER_NORMAL_REQ_CODE) {
if (filePathCallbackNormal == null) return;
Uri result = (data == null || resultCode != RESULT_OK) ? null : data.getData();
filePathCallbackNormal.onReceiveValue(result);
filePathCallbackNormal = null;
} else if (requestCode == ESavingConstants.FILECHOOSER_LOLLIPOP_REQ_CODE) {
Uri[] result = new Uri[0];
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
if(resultCode == RESULT_OK){
result = (data == null) ? new Uri[]{mCapturedImageURI} : WebChromeClient.FileChooserParams.parseResult(resultCode, data);
}
filePathCallbackLollipop.onReceiveValue(result);
}
}
}
Mainfest 퍼미션 설정
<!-- 5.0 버전 파일업로드 퍼미션 --> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <!-- 카메라 퍼미션 --> <uses-permission android:name="android.permission.CAMERA" /> <uses-feature android:name="android.hardware.camera" /> <!-- 외부 저장소 사용 --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> | c |
ESavingConstants.FILECHOOSER_NORMAL_REQ_CODE, ESavingConstants.FILECHOOSER_LOLLIPOP_REQ_CODE 이두 함수는 Int 타입으로서 1,2 값으로 대체하면 된다.
출처: http://cofs.tistory.com/182