在Android瀏覽器中通過WebView呼叫相機拍照/選擇檔案 上傳到伺服器
阿新 • • 發佈:2018-12-19
最近做的一個專案中,有這樣一個要求,在瀏覽器中呼叫系統的拍照功能或者選擇檔案,然後將檔案上傳到伺服器,類似修改頭像。 簡單而言,就是在一個html頁面中有這樣一段程式碼 <input class="filePrew" type="file" capture="camera" accept="image/*" name="image">
剛開始的時候,沒有感覺很難的,因為在UC瀏覽器、系統自帶的瀏覽器中都可以進行拍照/檔案管理器選擇,可是在自己所寫的Activity中卻不行。後來實在是沒有思路了,就在網上找了一下,發現要 實現這種功能,都是在webview的WebChromeClient中覆蓋掉openFileChooser方法,注意openFileChooser方法在WebChromeClient中有@hide標記。這裡只管重寫即可,下面將主要程式碼貼出來,做個記錄
- private ValueCallback<Uri> mUploadFile;
- /**拍照/選擇檔案請求碼*/
- privatestaticfinalint REQUEST_UPLOAD_FILE_CODE = 12343;
- privatevoid setWebChromeClient()
- {
- if (null != mMainWebView)
- {
- mMainWebView.setWebChromeClient(new WebChromeClient()
- {
- // Andorid 4.1+
- publicvoid openFileChooser(ValueCallback<Uri> uploadFile, String acceptType, String capture)
- {
- openFileChooser(uploadFile);
- }
- // Andorid 3.0 +
- publicvoid openFileChooser(ValueCallback<Uri> uploadFile, String acceptType)
- {
- openFileChooser(uploadFile);
- }
- // Android 3.0
- publicvoid openFileChooser(ValueCallback<Uri> uploadFile)
- {
- // Toast.makeText(WebviewActivity.this, "上傳檔案/圖片",Toast.LENGTH_SHORT).show();
- mUploadFile = uploadFile;
- startActivityForResult(Intent.createChooser(createCameraIntent(), "Image Browser"), REQUEST_UPLOAD_FILE_CODE);
- }
- });
- }
- }
- private Intent createCameraIntent()
- {
- Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//拍照
- //=======================================================
- Intent imageIntent = new Intent(Intent.ACTION_GET_CONTENT);//選擇圖片檔案
- imageIntent.setType("image/*");
- //=======================================================
- return cameraIntent;
- }
- //最後在OnActivityResult中接受返回的結果
- protectedvoid onActivityResult(int requestCode, int resultCode, Intent data)
- {
- if (requestCode == REQUEST_UPLOAD_FILE_CODE && resultCode == RESULT_OK)
- {
- if (null == mUploadFile)
- {
- return;
- }
- Uri result = (null == data) ? null : data.getData();
- if (null != result)
- {
- ContentResolver resolver = this.getContentResolver();
- String[] columns = { MediaStore.Images.Media.DATA };
- Cursor cursor = resolver.query(result, columns, null, null, null);
- cursor.moveToFirst();
- int columnIndex = cursor.getColumnIndex(columns[0]);
- String imgPath = cursor.getString(columnIndex);
- System.out.println("imgPath = " + imgPath);
- if (null == imgPath)
- {
- return;
- }
- File file = new File(imgPath);
- //將圖片處理成大小符合要求的檔案
- result = Uri.fromFile(handleFile(file));
- mUploadFile.onReceiveValue(result);
- mUploadFile = null;
- }
- }
- super.onActivityResult(requestCode, resultCode, data);
- }
- /**處理拍照/選擇的檔案*/
- private File handleFile(File file)
- {
- DisplayMetrics dMetrics = getResources().getDisplayMetrics();
- BitmapFactory.Options options = new Options();
- options.inJustDecodeBounds = true;
- BitmapFactory.decodeFile(file.getAbsolutePath(), options);
- int imageWidth = options.outWidth;
- int imageHeight = options.outHeight;
- System.out.println(" imageWidth = " + imageWidth + " imageHeight = " + imageHeight);
- int widthSample = (int) (imageWidth / (dMetrics.density * 90));
- int heightSample = (int) (imageHeight / (dMetrics.density * 90));
- System.out.println("widthSample = " + widthSample + " heightSample = " + heightSample);
- options.inSampleSize = widthSample < heightSample ? heightSample : widthSample;
- options.inJustDecodeBounds = false;
- Bitmap newBitmap = BitmapFactory.decodeFile(file.getAbsolutePath(), options);
- System.out.println("newBitmap.size = " + newBitmap.getRowBytes() * newBitmap.getHeight());
- File handleFile = new File(file.getParentFile(), "upload.png");
- try
- {
- if (newBitmap.compress(CompressFormat.PNG, 50, new FileOutputStream(handleFile)))
- {
- System.out.println("儲存圖片成功");
- }
- }
- catch (FileNotFoundException e)
- {
- e.printStackTrace();
- }
- return handleFile;
- }
http://blog.sina.com.cn/s/blog_5749ead90101clrn.html