처리방법의 중요골자는 openFileChooser 라는 hidden method를 통해 file tag을 인터셉트해서 앨범 액티비티를 띄운후 onActivityResult를 통해 사진 데이터를 받아 처리
//변수들
private ValueCallback<Uri> filePathCallbackNormal;
private ValueCallback<Uri[]> filePathCallbackLollipop;
private final static int FILECHOOSER_NORMAL_REQ_CODE = 1;
private final static int FILECHOOSER_LOLLIPOP_REQ_CODE = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView wv = (WebView) findViewById(R.id.webview);
wv.setWebChromeClient(new WebChromeClient() {
//여기부터 중요
// For Android < 3.0
public void openFileChooser( ValueCallback<Uri> uploadMsg) {
Log.d("MainActivity", "3.0 <");
openFileChooser(uploadMsg, "");
}
// For Android 3.0+
public void openFileChooser( ValueCallback<Uri> uploadMsg, String acceptType) {
Log.d("MainActivity", "3.0+");
filePathCallbackNormal = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_NORMAL_REQ_CODE);
}
// For Android 4.1+
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Log.d("MainActivity", "4.1+");
openFileChooser(uploadMsg, acceptType);
}
// For Android 5.0+
public boolean onShowFileChooser(
WebView webView, ValueCallback<Uri[]> filePathCallback,
WebChromeClient.FileChooserParams fileChooserParams) {
Log.d("MainActivity", "5.0+");
if (filePathCallbackLollipop != null) {
filePathCallbackLollipop.onReceiveValue(null);
filePathCallbackLollipop = null;
}
filePathCallbackLollipop = filePathCallback;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_LOLLIPOP_REQ_CODE);
return true;
}
//여기까지
});
WebSettings set = wv.getSettings();
set.setJavaScriptEnabled(true);
wv.loadUrl("http://test.com/upload.php");
}
//여기도 중요
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 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 == FILECHOOSER_LOLLIPOP_REQ_CODE) {
if (filePathCallbackLollipop == null) return ;
filePathCallbackLollipop.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, data));
filePathCallbackLollipop = null;
}
}
'Android' 카테고리의 다른 글
Intent를 이용한 카카오톡 공뷰하기 (0) | 2016.07.12 |
---|---|
ValueAnimator 를 이용한 색상 변경 애니메이션 (0) | 2016.07.12 |
Webview의 loadData와 loaddatawithbaseurl 의 차이점 (0) | 2016.07.12 |
CoordinatorLayout를 활용한 벅스뮤직 플레이 화면 같은 레이아웃 만들기 (0) | 2016.07.12 |
CoordinatorLayout.Behavior 를 이용한 동적 레이아웃 (0) | 2016.07.12 |