此代码用于网页浏览,我在该网页中提取网页页面有图像选择活动,上传一张图片而不是多张图片。我想上传多张图片。
请详细说明我的错误以及如何让工作图像选择Android版本为5.0 +
public class MainActivity extends Activity {
private static final int INPUT_FILE_REQUEST_CODE =1;
private static final int FILECHOOSER_RESULTCODE =2;
private static final String TAG =MainActivity.class.getSimpleName();
private WebView webView;
private WebSettings webSettings;
private ValueCallback<Uri> mUploadMessages;
private Uri mCapturedImageURI=null;
private ValueCallback<Uri[]> mFilePathCallback;
private String mCameraPhoto;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
if(requestCode==FILECHOOSER_RESULTCODE)
{
if (null == this.mUploadMessages) {
return;
}
Uri result=null;
try{
if (resultCode != RESULT_OK) {
result = null;
} else {
// retrieve from the private variable if the intent is null
result = intent == null ? mCapturedImageURI : intent.getData();
}
}
catch(Exception e)
{
Toast.makeText(getApplicationContext(), "activity :"+e,
Toast.LENGTH_LONG).show();
}
mUploadMessages.onReceiveValue(result);
mUploadMessages = null;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView=(WebView)findViewById(R.id.webview);
webSettings=webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setLoadWithOverviewMode(true);
webSettings.setAllowFileAccess(true);
webView.setWebViewClient(new WebViewClient());
webView.setWebChromeClient(new WebChromeClient());
if(Build.VERSION.SDK_INT >=19) {
webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
}
else if(Build.VERSION.SDK_INT>=11 && Build.VERSION.SDK_INT < 19){
webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
webView.loadUrl("http://m.multitechservice.co.in");
}
private File createImageFile() throws Exception{
String timeStamp=new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName= "JPEG_" + timeStamp + "_";
File storageDirectory=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File imageFile=File.createTempFile(imageFileName,".jpg",storageDirectory);
return imageFile;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
@SuppressWarnings("unused")
MenuInflater inflater=getMenuInflater();
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public class ChromeClient extends WebChromeClient{
@Override
public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback,
FileChooserParams fileChooserParams) {
// TODO Auto-generated method stub
if(mFilePathCallback != null){
mFilePathCallback.onReceiveValue(null);
}
mFilePathCallback=filePathCallback;
Intent takePictureIntent= new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if(takePictureIntent.resolveActivity(getPackageManager())!= null){
File photoFile=null;
try{
photoFile=createImageFile();
takePictureIntent.putExtra("Photo_Path", mCameraPhoto);
}catch(Exception ex){
Log.e(TAG, "Unable to create file path",ex);
}
if(photoFile!=null){
mCameraPhoto="file:" +photoFile.getAbsolutePath();
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
}else{
takePictureIntent=null;
}
}
Intent contentOnSelection=new Intent(Intent.ACTION_GET_CONTENT);
contentOnSelection.addCategory(Intent.CATEGORY_OPENABLE);
contentOnSelection.setType("image/*");
Intent[] intentArray;
if(takePictureIntent!=null){
intentArray=new Intent[]{takePictureIntent};
}else{
intentArray =new Intent[0];
}
Intent chooserIntent=new Intent(Intent.ACTION_CHOOSER);
chooserIntent.putExtra(Intent.EXTRA_INTENT, contentOnSelection);
chooserIntent.putExtra(Intent.EXTRA_TITLE, "Image Chooser");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
startActivityForResult(chooserIntent, INPUT_FILE_REQUEST_CODE);
return true;
}
public void openFileChooser(ValueCallback<Uri> uploadMSG,String acceptType){
mUploadMessages=uploadMSG;
File imageStorage=new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"Photo");
if(!imageStorage.exists()){
imageStorage.mkdirs();
}
File file=new File(imageStorage +File.separator +"IMG_" +String.valueOf(System.currentTimeMillis())+".jpg");
mCapturedImageURI=Uri.fromFile(file);
final Intent captureIntent=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);
Intent intent=new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
Intent chooserIntent=Intent.createChooser(intent, "IMAGE CHOOSER");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Parcelable[] {captureIntent});
startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE);
}
public void openFileChooser(ValueCallback<Uri> uploadMSG){
openFileChooser(uploadMSG," ");
}
public void openFileChooser(ValueCallback<Uri> uploadMsg,String acceptType,String capture){
openFileChooser(uploadMsg,acceptType);
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()){
webView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
public class Client extends WebViewClient {
ProgressDialog progressDialog;
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.contains("mailto:")) {
view.getContext().startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse(url)));
return true;
}
else{
view.loadUrl(url);
return true;
}
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
if(progressDialog == null){
progressDialog=new ProgressDialog(MainActivity.this);
progressDialog.setMessage("Loading.........");
progressDialog.show();
}
}
@Override
public void onPageFinished(WebView view, String url) {
try{
if(progressDialog.isShowing()){
progressDialog.dismiss();
progressDialog=null;
}
}catch(Exception e){
e.printStackTrace();
}
}
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
try{
view.stopLoading();
}catch(Exception ex){
ex.printStackTrace();
}
if(view.canGoBack()){
view.goBack();
}
view.loadUrl("about:blank");
AlertDialog alertDialog=new AlertDialog.Builder(MainActivity.this).create();
alertDialog.setTitle("Error");
alertDialog.setMessage("Cannot connect to Internet.Please check your internet connection");
alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "Try Again", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
startActivity(getIntent());
}
});
alertDialog.show();
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
if(id == R.id.action_refresh){
webView.reload();
return true;
}
return super.onOptionsItemSelected(item);
}
}