Input File Android -- JS 안 드 로 이 드 시스템 앨범 이 웹 뷰 에서 실 효 됨

[사진 업로드 중... (1)]http://blog.csdn.net/android_it/article/details/52538995 http://blog.csdn.net/android_it / article / details / 52538995 Android WebView 는 H5 input type = "file" 해결 방법 을 지원 하지 않 습 니 다.
Android webview JS 인 터 랙 션 으로 로 컬 앨범 을 가 져 오고 업로드 합 니 다.우선 우 리 는 전체 상호작용 과정 을 이해 해 야 한다. 웹 페이지 webview 의 구성 요 소 는 input 입력 상자 이 고 입력 유형 은 file 이 며 클릭 - - > 안 드 로 이 드 의 다른 버 전 을 호출 해 야 한다.
private static final int FILE_SELECT_CODE = 0; private ValueCallback mUploadMessage; private ValueCallback umUploadMessages; private WebChromeClient.FileChooserParams mfileChooserParams; private int FILECHOOSER_RESULTCODE_5 = 100; private int FILECHOOSER_RESULTCODE = 1000;
mWebView. setWebChromeClient (new WebChromeClient () {@ Override public boolean onJsAlert (WebView view, String url, String message, final JsResult result) {AlertDialog. Builder b2 = new AlertDialog. Builder (mContext). setTitle ("우정 알림:"). setMessage (message). setPositiveButton ("ok", new AlertDialog. OnClickListener (){ @Override public void onClick(DialogInterface dialog, int which) { result.confirm(); // MyWebView.this.finish(); } }); b2.setCancelable(false); b2.create(); b2.show(); return true; }
        @Override
        public boolean onShowFileChooser(WebView webView, ValueCallback filePathCallback, FileChooserParams fileChooserParams) {

            umUploadMessages = filePathCallback;
            mfileChooserParams = fileChooserParams;
            Intent i = new Intent(Intent.ACTION_GET_CONTENT);
            i.addCategory(Intent.CATEGORY_OPENABLE);
            i.putExtra("return-data", true);
            i.setType("image/*");
            i.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                    "image/*");
            startActivityForResult(Intent.createChooser(i, "    "),
                    FILECHOOSER_RESULTCODE_5);
            return true;
        }

        // For Android 3.0
        public void openFileChooser(ValueCallback uploadMsg) {
            mUploadMessage = uploadMsg;
            Intent i = new Intent(Intent.ACTION_GET_CONTENT);
            i.addCategory(Intent.CATEGORY_OPENABLE);
            i.putExtra("return-data", true);
            i.setType("image/*");
            i.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                    "image/*");

            startActivityForResult(Intent.createChooser(i, "    "),
                    FILECHOOSER_RESULTCODE);
        }

        // For Android > 4.1
        public void openFileChooser(ValueCallback uploadMsg,
                                    String acceptType, String capture) {
            mUploadMessage = uploadMsg;
            Intent i = new Intent(Intent.ACTION_GET_CONTENT);
            i.addCategory(Intent.CATEGORY_OPENABLE);
            i.putExtra("return-data", true);
            i.setType("image/*");
            i.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                    "image/*");
            startActivityForResult(Intent.createChooser(i, "    "),
                    FILECHOOSER_RESULTCODE);
        }

        // Andorid 3.0 +
        public void openFileChooser(ValueCallback uploadMsg, String acceptType) {
            mUploadMessage = uploadMsg;
            Intent i = new Intent(Intent.ACTION_GET_CONTENT);
            i.addCategory(Intent.CATEGORY_OPENABLE);
            i.putExtra("return-data", true);
            i.setType("image/*");
            i.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                    "image/*");
            startActivityForResult(Intent.createChooser(i, "    "),
                    FILECHOOSER_RESULTCODE);
        }
    });




@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == FILECHOOSER_RESULTCODE) {
        if (null == mUploadMessage) return;
        if (null == data) {
            mUploadMessage.onReceiveValue(null);
            mUploadMessage = null;
        } else {
            Uri result = data == null || resultCode != RESULT_OK ? null
                    : data.getData();
            mUploadMessage.onReceiveValue(result);
            mUploadMessage = null;
        }
    } else if (requestCode == FILECHOOSER_RESULTCODE_5) {// 5.0  ,     5.0       。
        //   5.0 callback
        if (umUploadMessages != null) {
            if (null != data) {
                // 5.0       ,   。
                ArrayList resultList = data
                        .getStringArrayListExtra("data");

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    umUploadMessages.onReceiveValue(
                            mfileChooserParams.parseResult(resultCode, data));
                }
                umUploadMessages = null;
            } else {
                umUploadMessages.onReceiveValue(null);
            }
        }
    }
}

참고 글:http://blog.csdn.net/android_it/article/details/52538995
private WebView wv; private String url ="http://shanghai.job1s.com/wap/member/index.php?c=photo";
private ValueCallback mUploadMessage;
public ValueCallback uploadMessage;
public static final int REQUEST_SELECT_FILE = 100;
private final static int FILECHOOSER_RESULTCODE = 2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    wv = (WebView) findViewById(R.id.webview);
    //    Javascript
    wv.getSettings().setJavaScriptEnabled(true);  

    wv.loadUrl(url);
    wv.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            //    true      WebView  , false              
            view.loadUrl(url);
            return false;
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
        }

        @Override
        public void onPageFinished(WebView view, String url) {
        }
    });


    wv.setWebChromeClient(new WebChromeClient(){

        // For 3.0+ Devices (Start)
        // onActivityResult attached before constructor
        protected void openFileChooser(ValueCallback uploadMsg, String acceptType)
        {
            mUploadMessage = uploadMsg;
            Intent i = new Intent(Intent.ACTION_GET_CONTENT);
            i.addCategory(Intent.CATEGORY_OPENABLE);
            i.setType("image/*");
            startActivityForResult(Intent.createChooser(i, "File Browser"), FILECHOOSER_RESULTCODE);
        }


        // For Lollipop 5.0+ Devices
        @TargetApi(Build.VERSION_CODES.LOLLIPOP)
        public boolean onShowFileChooser(WebView mWebView, ValueCallback filePathCallback, WebChromeClient.FileChooserParams fileChooserParams)
        {
            if (uploadMessage != null) {
                uploadMessage.onReceiveValue(null);
                uploadMessage = null;
            }

            uploadMessage = filePathCallback;

            Intent intent = fileChooserParams.createIntent();
            try
            {
                startActivityForResult(intent, REQUEST_SELECT_FILE);
            } catch (ActivityNotFoundException e)
            {
                uploadMessage = null;
                Toast.makeText(getBaseContext(), "Cannot Open File Chooser", Toast.LENGTH_LONG).show();
                return false;
            }
            return true;
        }

        //For Android 4.1 only
        protected void openFileChooser(ValueCallback uploadMsg, String acceptType, String capture)
        {
            mUploadMessage = uploadMsg;
            Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
            intent.addCategory(Intent.CATEGORY_OPENABLE);
            intent.setType("image/*");
            startActivityForResult(Intent.createChooser(intent, "File Browser"), FILECHOOSER_RESULTCODE);
        }

        protected void openFileChooser(ValueCallback uploadMsg)
        {
            mUploadMessage = uploadMsg;
            Intent i = new Intent(Intent.ACTION_GET_CONTENT);
            i.addCategory(Intent.CATEGORY_OPENABLE);
            i.setType("image/*");
            startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE);
        }

    });

}


@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent)
{

    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
    {
        if (requestCode == REQUEST_SELECT_FILE)
        {
            if (uploadMessage == null)
                return;
            uploadMessage.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, intent));
            uploadMessage = null;
        }
    }
    else if (requestCode == FILECHOOSER_RESULTCODE)
    {
        if (null == mUploadMessage)
            return;
        // Use MainActivity.RESULT_OK if you're implementing WebView inside Fragment
        // Use RESULT_OK only if you're implementing WebView inside an Activity
        Uri result = intent == null || resultCode != MainActivity.RESULT_OK ? null : intent.getData();
        mUploadMessage.onReceiveValue(result);
        mUploadMessage = null;
    }
    else
        Toast.makeText(getBaseContext(), "Failed to Upload Image", Toast.LENGTH_LONG).show();
}

android webview html 5 로 앨범 업로드, 사진 찍 기

좋은 웹페이지 즐겨찾기