도토리세상

웹뷰에선 버전별로 파일 첨부 코드를 생성해 줘야 한다.

*다른 버전은 문제 없는거 같은데 4.2.2 버전에서는 안된다.(이유는 모르겠다.)


permission 추가

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />


변수 선언

private static final int FILECHOOSER_RESULTCODE   = 1;
private final static int FILECHOOSER_LOLLIPOP_REQ_CODE = 2;
private ValueCallback<Uri> mUploadMessage = null;
private ValueCallback<Uri[]> filePathCallbackLollipop;


WebView setWebChromeClient 부분

// For Android 3.0-
@SuppressWarnings("unused")
public void openFileChooser(ValueCallback<Uri> uploadMsg) {
mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
//i.setType("image/*");
i.setType("*/*");
startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE);
}

// For Android 3.0+
@SuppressWarnings("unused")
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("*/*");
startActivityForResult(Intent.createChooser(i, "File Browser"), FILECHOOSER_RESULTCODE);
}

// For Android 4.1+
@SuppressWarnings("unused")
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
//i.setType("image/*");
i.setType("*/*");
startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE);
}

// 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;
}


결과값 처리

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == FILECHOOSER_RESULTCODE) {
if (null == mUploadMessage)
return;
Uri result = intent == null || resultCode != RESULT_OK ? null : intent.getData();
mUploadMessage.onReceiveValue(result);
mUploadMessage = null;
} else if (requestCode == FILECHOOSER_LOLLIPOP_REQ_CODE) {
if (filePathCallbackLollipop == null) return ;
filePathCallbackLollipop.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, intent));
filePathCallbackLollipop = null;
}
}


Comment +1

  • 니버 2017.02.09 23:29

    package com.sejaptu.application;

    import android.content.Intent;
    import android.net.Uri;
    import android.support.v7.app.AppCompatActivity;
    import android.annotation.SuppressLint;
    import android.os.Bundle;
    import android.test.suitebuilder.annotation.Suppress;
    import android.util.Log;
    import android.view.KeyEvent;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.webkit.ValueCallback;
    import android.webkit.WebChromeClient;
    import android.webkit.WebView;
    import android.webkit.WebViewClient;

    @SuppressLint("SetJavaScriptEnabled";)

    public class MainActivity extends AppCompatActivity {

    private WebView mWebView; // 웹뷰 선언
    private static final int FILECHOOSER_RESULTCODE = 1;
    private static final int FILECHOOSER_LOLLIPOP_REQ_CODE = 2;
    private ValueCallback<Uri> mUploadMessage = null;
    private ValueCallback<Uri[]> filePathCallbackLollipop;

    //For Android 3.0-
    @SuppressWarnings("unused";)
    public void openFileChooser(ValueCallback<Uri> uploadMsg) {
    mUploadMessage = uploadMsg;
    Intent i = new Intent(Intent.ACTION_GET_CONTENT);
    i.addCategory(Intent.CATEGORY_OPENABLE);
    //i.setType("image/*";);
    i.setType("*/*";);
    startActivityForResult(Intent.createChooser(i, "File Chooser";), FILECHOOSER_RESULTCODE);
    }

    //For Android 3.0+
    @SuppressWarnings("unused";)
    public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
    mUploadMessage = uploadMsg;
    Intent i = new Intent(Intent.ACTION_GET_CONTENT);
    i.addCategory(Intent.CATEGORY_OPENABLE);
    i.setType("*/*";);
    startActivityForResult(Intent.createChooser(i, "File Brooser";), FILECHOOSER_RESULTCODE);
    }

    //For Android 4.1+
    @SuppressWarnings("unused";)
    public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
    mUploadMessage = uploadMsg;
    Intent i = new Intent(Intent.ACTION_GET_CONTENT);
    i.addCategory(Intent.CATEGORY_OPENABLE);
    //i.setType("image/*";);
    i.setType("*/*";);
    startActivityForResult(Intent.createChooser(i, "File Chooser";), FILECHOOSER_RESULTCODE);
    }

    //For Android 5.0+
    @SuppressWarnings("unused";)
    public boolean openFileChooser(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;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

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

    mWebView.getSettings().setJavaScriptEnabled(true); // 웹뷰에서 자바 스크립트 사용

    mWebView.loadUrl("http://buffett.kr/";); // 웹뷰에서 불러올 URL 입력

    mWebView.setWebViewClient(new WishWebViewClient());

    }

    @Override

    public boolean onKeyDown(int keyCode, KeyEvent event) {

    if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {

    mWebView.goBack();

    return true;

    }

    return super.onKeyDown(keyCode, event);

    }


    private class WishWebViewClient extends WebViewClient {

    @Override

    public boolean shouldOverrideUrlLoading(WebView view, String url) {

    view.loadUrl(url);

    return true;

    }

    @Override

    protected void onAcivityResult(int requestCode, int resultCode, Intent intent) {
    if (requestCode == FILECHOOSER_RESULTCODE) {
    if (null == mUploadMessage)
    return;
    Uri result = intent == null || resultCode != RESULT_OK ? null : intent.getData();
    mUploadMessage.onReceiveValue(result);
    mUploadMessage = null;
    } else if (requestCode == FILECHOOSER_LOLLIPOP_REQ_CODE) {
    if (filePathCallbackLollipop == null) return;
    filePathCallbackLollipop.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, intent));

    filePathCallbackLollipop = null;
    }
    }
    }
    }

    무엇이 잘 못 되었는지 봐 주실수 있나요?