使用Webview将站点迁移到Android应用程序

时间:2015-04-13 08:07:40

标签: android webview android-camera

我使用webview将我的网站成功迁移到了一个Android应用程序。我的网站要求您上传照片,但无法从我的应用访问本地文件或Android相机。我如何使其工作?

这是我的MainActivity.java

package com.example.oscar.lostnfound;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Environment;
import android.os.Parcelable;
import android.provider.MediaStore;
import android.view.View;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.ConsoleMessage;
import android.webkit.JavascriptInterface;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.Toast;

import java.io.File;

public class MainActivity extends Activity {
private WebView webView;
private ProgressBar progress;
Button button;
final Activity activity = this;
public Uri imageUri;

public static final int FILECHOOSER_RESULTCODE  = 2888;
public ValueCallback<Uri> mUploadMessage;
public Uri mCapturedImageURI = null;
// @Override

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.webview);

    webView = (WebView) findViewById(R.id.webView);
    //webView.setWebViewClient(new MyWebViewClient());
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setLoadWithOverviewMode(true);
    //other webview settings
    webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
    webView.setScrollbarFadingEnabled(false);
    webView.getSettings().setBuiltInZoomControls(true);
    webView.getSettings().setPluginState(WebSettings.PluginState.ON);
    webView.getSettings().setAllowFileAccess(true);
    webView.getSettings().setSupportZoom(true);


    String url = "http://codeupset.com/academy-form";
    webView.loadUrl(url);
    progress = (ProgressBar) findViewById(R.id.progress);

    startWebView();
}

@Override
public void onBackPressed() {
    if (webView.canGoBack()) {
        webView.goBack();
    } else {
        super.onBackPressed();
    }
}
  @JavascriptInterface
   public void startWebView(){

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

                               }

                               boolean bReceivedError = false;

       //@Override
       public void onReceivedError(WebView view, int errorCode,
                                   String description, String failingUrl) {
           bReceivedError = true;
           setContentView(R.layout.error);
           button = (Button) findViewById(R.id.button);
           Toast.makeText(getApplicationContext(), "Check Internet connection",
                   Toast.LENGTH_LONG).show();
           //On click listener
           View.OnClickListener retry = new View.OnClickListener() {
               @Override
               public void onClick(View v) {

                   webView = (WebView) findViewById(R.id.webView);
                   setContentView(R.layout.webview);
                   String url = "http://codeupset.com";
                   webView.loadUrl(url);
               }
           };
           // set OnClickListner for the button
           button.setOnClickListener(retry);
       }

       //@Override
       public void onPageFinished(WebView view, String url) {
           progress.setVisibility(View.GONE);
           MainActivity.this.progress.setProgress(100);
           super.onPageFinished(view, url);
       }

      // @Override
       public void onPageStarted(WebView view, String url, Bitmap favicon) {
           progress.setVisibility(View.VISIBLE);
           MainActivity.this.progress.setProgress(0);
           super.onPageStarted(view, url, favicon);
                               }

 });


          webView.setWebChromeClient(new WebChromeClient() {

              // openFileChooser for Android 3.0+
              @JavascriptInterface
              public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {

                  // Update message
                  mUploadMessage = uploadMsg;

                  try {

                      // Create AndroidExampleFolder at sdcard

                      File imageStorageDir = new File(
                              Environment.getExternalStoragePublicDirectory(
                                      Environment.DIRECTORY_PICTURES)
                              , "AndroidExampleFolder");

                      if (!imageStorageDir.exists()) {
                          // Create AndroidExampleFolder at sdcard
                          imageStorageDir.mkdirs();
                      }

                      // Create camera captured image file path and name
                      File file = new File(
                              imageStorageDir + File.separator + "IMG_"
                                      + String.valueOf(System.currentTimeMillis())
                                    + ".jpg");

                      mCapturedImageURI = Uri.fromFile(file);

                     // Camera capture image intent
                      final Intent captureIntent = new Intent(
                              android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

                      captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);

                      Intent i = new Intent(Intent.ACTION_GET_CONTENT);
                      i.addCategory(Intent.CATEGORY_OPENABLE);
                      i.setType("image/*");

                      // Create file chooser intent
                      Intent chooserIntent = Intent.createChooser(i, "Image Chooser");

                      // Set camera intent to file chooser
                      chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS
                              , new Parcelable[]{captureIntent});

                      // On select image call onActivityResult method of activity
                      startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE);

                  } catch (Exception e) {
                      Toast.makeText(getBaseContext(), "Exception:" + e,
                              Toast.LENGTH_LONG).show();
                  }

              }

              // openFileChooser for Android < 3.0
             @JavascriptInterface
              public void openFileChooser(ValueCallback<Uri> uploadMsg) {
                  openFileChooser(uploadMsg, "");
              }

              //openFileChooser for other Android versions
              @JavascriptInterface
              public void openFileChooser(ValueCallback<Uri> uploadMsg,
                                          String acceptType,
                                          String capture) {

                  openFileChooser(uploadMsg, acceptType);
              }


              // The webPage has 2 filechoosers and will send a
              // console message informing what action to perform,
              // taking a photo or updating the file

              public boolean onConsoleMessage(ConsoleMessage cm) {

                  onConsoleMessage(cm.message(), cm.lineNumber(), cm.sourceId());
                  return true;
              }

          });   // End setWebChromeClient

    }



// Return here when file selected from camera or from SDcard

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

    if(requestCode==FILECHOOSER_RESULTCODE)
    {

    if (null == this.mUploadMessage) {
    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();
    }

    mUploadMessage.onReceiveValue(result);
    mUploadMessage = null;

    }

    }




@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}
public void setValue(int progress) {
    this.progress.setProgress(progress);
}

@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();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
 }
}

这是我的Xml(webview.xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">

<WebView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/webView"
    android:layout_gravity="center_vertical" />

<ProgressBar
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/progress"
    android:layout_centerVertical="true"
    android:indeterminate="false" />



</RelativeLayout>

logcat的

&#13;
&#13;
04-16 09:30:56.116  28887-28887/com.example.oscar.lostnfound I/SnapScrollController﹕ setSnapScrollingMode case-default no-op
04-16 09:30:56.251  28887-28887/com.example.oscar.lostnfound I/SnapScrollController﹕ setSnapScrollingMode case-default no-op
04-16 09:30:56.266  28887-28887/com.example.oscar.lostnfound I/ScaleGestureDetector﹕ TwScaleGestureDetector
04-16 09:31:58.906  28887-28887/com.example.oscar.lostnfound W/IInputConnectionWrapper﹕ showStatusIcon on inactive InputConnection
04-16 09:32:42.196  30041-30041/com.example.oscar.lostnfound W/ApplicationPackageManager﹕ getCSCPackageItemText()
04-16 09:32:42.201  30041-30041/com.example.oscar.lostnfound I/PersonaManager﹕ getPersonaService() name persona_policy
04-16 09:32:42.281  30041-30041/com.example.oscar.lostnfound V/WebViewChromium﹕ Binding Chromium to the background looper Looper (main, tid 1) {431a6968}
04-16 09:32:42.286  30041-30041/com.example.oscar.lostnfound I/chromium﹕ [INFO:library_loader_hooks.cc(112)] Chromium logging enabled: level = 0, default verbosity = 0
04-16 09:32:42.286  30041-30041/com.example.oscar.lostnfound I/BrowserProcessMain﹕ Initializing chromium process, renderers=0
04-16 09:32:42.301  30041-30072/com.example.oscar.lostnfound W/chromium﹕ [WARNING:proxy_service.cc(888)] PAC support disabled because there is no system implementation
04-16 09:32:42.301  30041-30041/com.example.oscar.lostnfound W/ApplicationPackageManager﹕ getCSCPackageItemText()
04-16 09:32:42.656  30041-30041/com.example.oscar.lostnfound D/OpenGLRenderer﹕ Enabling debug mode 0
04-16 09:32:42.671  30041-30041/com.example.oscar.lostnfound D/ProgressBar﹕ updateDrawableBounds: left = 468
04-16 09:32:42.671  30041-30041/com.example.oscar.lostnfound D/ProgressBar﹕ updateDrawableBounds: top = 0
04-16 09:32:42.671  30041-30041/com.example.oscar.lostnfound D/ProgressBar﹕ updateDrawableBounds: right = 612
04-16 09:32:42.671  30041-30041/com.example.oscar.lostnfound D/ProgressBar﹕ updateDrawableBounds: bottom = 144
04-16 09:32:42.681  30041-30041/com.example.oscar.lostnfound W/AwContents﹕ nativeOnDraw failed; clearing to background color.
04-16 09:32:42.941  30041-30041/com.example.oscar.lostnfound I/chromium﹕ [INFO:async_pixel_transfer_manager_android.cc(56)] Async pixel transfers not supported
04-16 09:32:43.016  30041-30041/com.example.oscar.lostnfound I/chromium﹕ [INFO:async_pixel_transfer_manager_android.cc(56)] Async pixel transfers not supported
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

  

试试这个,

activity_main.xml: -

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              android:id="@+id/LinearLayout1"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical"
              android:padding="10dp" >

  <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:gravity="center"
          android:padding="5dp" >

    <Button
            android:id="@+id/btnSelectPhoto"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Select Photo" />

  </LinearLayout>

  <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical"
          android:padding="10dp" >

    <ImageView
            android:id="@+id/viewImage"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

  </LinearLayout>

</LinearLayout> 
  

现在创建这样的主要活动: -

 public class MainActivity extends Activity {

    ImageView viewImage;
    Button b;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        b=(Button)findViewById(R.id.btnSelectPhoto);
        viewImage=(ImageView)findViewById(R.id.viewImage);
        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectImage();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds options to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

      private void selectImage() {

        final CharSequence[] options = { "Take Photo", "Choose from Gallery","Cancel" };

        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setTitle("Add Photo!");
        builder.setItems(options, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int item) {
                if (options[item].equals("Take Photo"))
                {
                    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    File f = new File(android.os.Environment.getExternalStorageDirectory(), "temp.jpg");
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
                    startActivityForResult(intent, 1);
                }
                else if (options[item].equals("Choose from Gallery"))
                {
                    Intent intent = new   Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    startActivityForResult(intent, 2);

                }
                else if (options[item].equals("Cancel")) {
                    dialog.dismiss();
                }
            }
        });
        builder.show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == 1) {
                File f = new File(Environment.getExternalStorageDirectory().toString());
                for (File temp : f.listFiles()) {
                    if (temp.getName().equals("temp.jpg")) {
                        f = temp;
                        break;
                    }
                }
                try {
                    Bitmap bitmap;
                    BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();

                    bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(),
                            bitmapOptions); 

                    viewImage.setImageBitmap(bitmap);

                    String path = android.os.Environment
                            .getExternalStorageDirectory()
                            + File.separator
                            + "Phoenix" + File.separator + "default";
                    f.delete();
                    OutputStream outFile = null;
                    File file = new File(path, String.valueOf(System.currentTimeMillis()) + ".jpg");
                    try {
                        outFile = new FileOutputStream(file);
                        bitmap.compress(Bitmap.CompressFormat.JPEG, 85, outFile);
                        outFile.flush();
                        outFile.close();
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else if (requestCode == 2) {

                Uri selectedImage = data.getData();
                String[] filePath = { MediaStore.Images.Media.DATA };
                Cursor c = getContentResolver().query(selectedImage,filePath, null, null, null);
                c.moveToFirst();
                int columnIndex = c.getColumnIndex(filePath[0]);
                String picturePath = c.getString(columnIndex);
                c.close();
                Bitmap thumbnail = (BitmapFactory.decodeFile(picturePath));
                Log.w("path of image from gallery......******************.........", picturePath+"");
                viewImage.setImageBitmap(thumbnail);
            }
        }
    }   
}

答案 1 :(得分:0)

  

我告诉你了......试试这个,

public class ShowWebView extends Activity {

    //private Button button;
    private WebView webView;
    final Activity activity = this;
    public Uri imageUri;

    private static final int FILECHOOSER_RESULTCODE   = 2888;
    private ValueCallback<Uri> mUploadMessage;
    private Uri mCapturedImageURI = null;


    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.show_web_view);

        //Get webview 
        webView = (WebView) findViewById(R.id.webView1);

        // Define url that will open in webview 
        String webViewUrl = "http://www.yourWebsite.com";



        // Javascript inabled on webview  
        webView.getSettings().setJavaScriptEnabled(true);

        // Other webview options
        webView.getSettings().setLoadWithOverviewMode(true);

        //webView.getSettings().setUseWideViewPort(true);

        //Other webview settings
        webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
        webView.setScrollbarFadingEnabled(false);
        webView.getSettings().setBuiltInZoomControls(true);
        webView.getSettings().setPluginState(PluginState.ON);
        webView.getSettings().setAllowFileAccess(true);
        webView.getSettings().setSupportZoom(true); 

        //Load url in webview
        webView.loadUrl(webViewUrl);

        // Define Webview manage classes
        startWebView(); 

    } 

    private void startWebView() {



        // Create new webview Client to show progress dialog
        // Called When opening a url or click on link
        // You can create external class extends with WebViewClient 
        // Taking WebViewClient as inner class

        webView.setWebViewClient(new WebViewClient() {      
            ProgressDialog progressDialog;

            //If you will not use this method url links are open in new brower not in webview
            public boolean shouldOverrideUrlLoading(WebView view, String url) {              

                // Check if Url contains ExternalLinks string in url 
                // then open url in new browser
                // else all webview links will open in webview browser
                if(url.contains("google")){ 

                    // Could be cleverer and use a regex
                    //Open links in new browser
                    view.getContext().startActivity(
                            new Intent(Intent.ACTION_VIEW, Uri.parse(url)));

                    // Here we can open new activity

                    return true;

                } else {

                    // Stay within this webview and load url
                    view.loadUrl(url); 
                    return true;
                }

            }



            //Show loader on url load
            public void onLoadResource (WebView view, String url) {

                // if url contains string androidexample
                // Then show progress  Dialog
                if (progressDialog == null && url.contains("androidexample") 
                        ) {

                    // in standard case YourActivity.this
                    progressDialog = new ProgressDialog(ShowWebView.this);
                    progressDialog.setMessage("Loading...");
                    progressDialog.show();
                }
            }

            // Called when all page resources loaded
            public void onPageFinished(WebView view, String url) {

                try{
                    // Close progressDialog
                    if (progressDialog.isShowing()) {
                        progressDialog.dismiss();
                        progressDialog = null;
                    }
                }catch(Exception exception){
                    exception.printStackTrace();
                }
            }

        }); 


        // You can create external class extends with WebChromeClient 
        // Taking WebViewClient as inner class
        // we will define openFileChooser for select file from camera or sdcard

        webView.setWebChromeClient(new WebChromeClient() {

            // openFileChooser for Android 3.0+
            public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType){  

                // Update message
                mUploadMessage = uploadMsg;

                try{    

                    // Create AndroidExampleFolder at sdcard

                    File imageStorageDir = new File(
                                           Environment.getExternalStoragePublicDirectory(
                                           Environment.DIRECTORY_PICTURES)
                                           , "AndroidExampleFolder");

                    if (!imageStorageDir.exists()) {
                        // Create AndroidExampleFolder at sdcard
                        imageStorageDir.mkdirs();
                    }

                    // Create camera captured image file path and name 
                    File file = new File(
                                    imageStorageDir + File.separator + "IMG_" 
                                    + String.valueOf(System.currentTimeMillis()) 
                                    + ".jpg");

                    mCapturedImageURI = Uri.fromFile(file); 

                    // Camera capture image intent
                    final Intent captureIntent = new Intent(
                                                  android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

                    captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);

                    Intent i = new Intent(Intent.ACTION_GET_CONTENT); 
                    i.addCategory(Intent.CATEGORY_OPENABLE);
                    i.setType("image/*");

                    // Create file chooser intent
                    Intent chooserIntent = Intent.createChooser(i, "Image Chooser");

                    // Set camera intent to file chooser 
                    chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS
                                           , new Parcelable[] { captureIntent });

                    // On select image call onActivityResult method of activity
                    startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE);

                  }
                 catch(Exception e){
                     Toast.makeText(getBaseContext(), "Exception:"+e, 
                                Toast.LENGTH_LONG).show();
                 }

            }

            // openFileChooser for Android < 3.0
            public void openFileChooser(ValueCallback<Uri> uploadMsg){
                openFileChooser(uploadMsg, "");
            }

            //openFileChooser for other Android versions
            public void openFileChooser(ValueCallback<Uri> uploadMsg, 
                                       String acceptType, 
                                       String capture) {

                openFileChooser(uploadMsg, acceptType);
            }



            // The webPage has 2 filechoosers and will send a 
            // console message informing what action to perform, 
            // taking a photo or updating the file

            public boolean onConsoleMessage(ConsoleMessage cm) {  

                onConsoleMessage(cm.message(), cm.lineNumber(), cm.sourceId());
                return true;
            }

            public void onConsoleMessage(String message, int lineNumber, String sourceID) {
                //Log.d("androidruntime", "Show console messages, Used for debugging: " + message);

            }
        });   // End setWebChromeClient

    }



    // Return here when file selected from camera or from SDcard

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

     if(requestCode==FILECHOOSER_RESULTCODE)  
     {  

            if (null == this.mUploadMessage) {
                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();
            }

            mUploadMessage.onReceiveValue(result);
            mUploadMessage = null;

     }

    }

    // Open previous opened link from history on webview when back button pressed

    @Override
    // Detect when the back button is pressed
    public void onBackPressed() {

        if(webView.canGoBack()) {

            webView.goBack();

        } else {
            // Let the system handle the back button
            super.onBackPressed();
        }
    }

}
  

希望你的xml文件是这样的,

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

   <WebView  
    android:id="@+id/webView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
/>       
</LinearLayout>
  

还会在清单中添加这些权限。

<uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-feature android:name="android.hardware.camera" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />