privatevoidimageChooser(){IntenttakePictureIntent=newIntent(MediaStore.ACTION_IMAGE_CAPTURE);if(takePictureIntent.resolveActivity(getContext().getPackageManager())!=null){// Create the File where the photo should goFilephotoFile=null;try{photoFile=createImageFile();takePictureIntent.putExtra("PhotoPath",mCameraPhotoPath);}catch(IOExceptionex){// Error occurred while creating the FileLog.e(getClass().getName(),"Unable to create Image File",ex);}// Continue only if the File was successfully createdif(photoFile!=null){mCameraPhotoPath="file:"+photoFile.getAbsolutePath();takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(photoFile));}else{takePictureIntent=null;}}IntentcontentSelectionIntent=newIntent(Intent.ACTION_GET_CONTENT);contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);contentSelectionIntent.setType(TYPE_IMAGE);Intent[]intentArray;if(takePictureIntent!=null){intentArray=newIntent[]{takePictureIntent};}else{intentArray=newIntent[0];}IntentchooserIntent=newIntent(Intent.ACTION_CHOOSER);chooserIntent.putExtra(Intent.EXTRA_INTENT,contentSelectionIntent);chooserIntent.putExtra(Intent.EXTRA_TITLE,"Image Chooser");chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,intentArray);startActivityForResult(chooserIntent,INPUT_FILE_REQUEST_CODE);}
privateUrigetResultUri(Intentdata){Uriresult=null;if(data==null||TextUtils.isEmpty(data.getDataString())){// If there is not data, then we may have taken a photoif(mCameraPhotoPath!=null){result=Uri.parse(mCameraPhotoPath);}}else{StringfilePath="";if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP){filePath=data.getDataString();}else{filePath="file:"+RealPathUtil.getRealPath(getContext(),data.getData());}result=Uri.parse(filePath);}returnresult;}
バージョン別
1. Android Version 5.0+
12345678910
publicbooleanonShowFileChooser(WebViewwebView,ValueCallback<Uri[]>filePathCallback,FileChooserParamsfileChooserParams){System.out.println("WebViewActivity A>5, OS Version : "+Build.VERSION.SDK_INT+"\t onSFC(WV,VCUB,FCP), n=3");if(filePathCallbackLollipop!=null){filePathCallbackLollipop.onReceiveValue(null);}filePathCallbackLollipop=filePathCallback;imageChooser();returntrue;}