讓Android的WebView支援html裡面的檔案上傳

來源:互聯網
上載者:User

標籤:

預設情況下,Android的webview是不支援<input type=file>的,點擊沒有任何反應,如果希望點擊上傳,彈出選擇檔案、圖片的視窗,那就需要自訂一個WebChromeClient

public class MyChromeClient extends WebChromeClient {    public ValueCallback<Uri> UploadMsg;    public ValueCallback<Uri[]> UploadMsg2;    private Activity activity;    public static final int FILECHOOSER_RESULTCODE = 5173;    public static String mCameraFilePath = "";    @SuppressWarnings("deprecation")    public MyChromeClient(Activity cordova) {        this.activity = cordova;    }    @Override    public void onProgressChanged(WebView view, int newProgress) {        super.onProgressChanged(view, newProgress);    }            // <input type="file" name="fileField" id="fileField" />    // Android > 4.1.1    @Override    public boolean onShowFileChooser(WebView webView,            ValueCallback<Uri[]> filePathCallback,            FileChooserParams fileChooserParams) {        // TODO 自動產生的方法存根        UploadMsg2 = filePathCallback;        this.activity.startActivityForResult(createDefaultOpenableIntent(),                this.FILECHOOSER_RESULTCODE);        return false;    }    @SuppressWarnings("static-access")    public void openFileChooser(ValueCallback<Uri> uploadMsg,    String acceptType, String capture) {        UploadMsg = uploadMsg;        this.activity.startActivityForResult(createDefaultOpenableIntent(),        this.FILECHOOSER_RESULTCODE);    }    // 3.0 +    @SuppressWarnings("static-access")    public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {        UploadMsg = uploadMsg;        this.activity.startActivityForResult(createDefaultOpenableIntent(),        this.FILECHOOSER_RESULTCODE);    }    // Android < 3.0    @SuppressWarnings("static-access")    public void openFileChooser(ValueCallback<Uri> uploadMsg) {        UploadMsg = uploadMsg;        this.activity.startActivityForResult(createDefaultOpenableIntent(),        this.FILECHOOSER_RESULTCODE);    }    private Intent createDefaultOpenableIntent() {        Intent i = new Intent(Intent.ACTION_GET_CONTENT);        i.addCategory(Intent.CATEGORY_OPENABLE);        i.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,        "image/*");        Intent chooser = createChooserIntent(createCameraIntent()/*                                                                 *                                                                  * ,                                                                 *                                                                  * createCamcorderIntent                                                                 *                                                                  * (),                                                                 *                                                                  * createSoundRecorderIntent                                                                 *                                                                  * ()                                                                 */);        chooser.putExtra(Intent.EXTRA_INTENT, i);        return chooser;    }    private Intent createChooserIntent(Intent... intents) {        Intent chooser = new Intent(Intent.ACTION_CHOOSER);        chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, intents);        chooser.putExtra(Intent.EXTRA_TITLE, "選擇圖片");        return chooser;    }    @SuppressWarnings("static-access")    private Intent createCameraIntent() {        Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);        File externalDataDir = Environment        .getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);        File cameraDataDir = new File(externalDataDir.getAbsolutePath()        + File.separator + "515aaa");        cameraDataDir.mkdirs();        String mCameraFilePath = cameraDataDir.getAbsolutePath()        + File.separator + System.currentTimeMillis() + ".jpg";        this.mCameraFilePath = mCameraFilePath;        cameraIntent.putExtra(MediaStore.Images.Media.ORIENTATION, 0);        cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,        Uri.fromFile(new File(mCameraFilePath)));        return cameraIntent;    }    /*     *      * private Intent createCamcorderIntent() { return new     *      * Intent(MediaStore.ACTION_VIDEO_CAPTURE); }     *      *      *      * private Intent createSoundRecorderIntent() { return new     *      * Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION); }     */    public static Uri getImageContentUri(Context context, java.io.File imageFile) {        String filePath = imageFile.getAbsolutePath();        Cursor cursor = context.getContentResolver().query(        MediaStore.Images.Media.EXTERNAL_CONTENT_URI,        new String[] { MediaStore.Images.Media._ID },        MediaStore.Images.Media.DATA + "=? ",        new String[] { filePath }, null);        if (cursor != null && cursor.moveToFirst()) {            int id = cursor.getInt(cursor            .getColumnIndex(MediaStore.MediaColumns._ID));            Uri baseUri = Uri.parse("content://media/external/images/media");            return Uri.withAppendedPath(baseUri, "" + id);        } else {            if (imageFile.exists()) {                ContentValues values = new ContentValues();                values.put(MediaStore.Images.Media.DATA, filePath);                return context.getContentResolver().insert(                MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);            } else {                return null;            }        }    }}

然後指定webview使用這個client

webview.setWebChromeClient( new MyChromeClient (this));

 

讓Android的WebView支援html裡面的檔案上傳

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.