保存WebView加载另一个活动?

时间:2011-05-18 07:36:35

标签: android android-activity android-webview

我有一个webview,我加载了一个外部URL,所以这加载了第一页,我点击链接转到第二页然后我点击第三页。

现在在第三页上,我已经编写了一个Javascript来打开另一个可以正常工作的Activity。 但是在这个Activity的关闭之后,我的WebView再次重新加载了第一页,它应该是调用Activity的同一页面。??

任何关于我做错了什么的想法?

先谢谢。

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import android.R.bool;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Base64;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;

import android.widget.Toast;

public class Cam2 extends Activity {
    int CAMERA_PIC_REQUEST = 2;

    WebView webview;
    String imageSource;
    boolean _hasImage = false;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        webview = (WebView) findViewById(R.id.webview);
        webview.getSettings().setJavaScriptEnabled(true);

        final Activity activity = this;
        webview.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress) {
                activity.setProgress(progress * 1000);
            }
        });
        webview.setWebViewClient(new WebViewClient() {
            public void onReceivedError(WebView view, int errorCode,
                    String description, String failingUrl) {
                Toast.makeText(activity, "Oh no! " + description,
                        Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                if (_hasImage) {
                    webview.loadUrl("javascript:AndroidSubmitToWeb();");
                }
            }
        });

        webview.addJavascriptInterface(new JavaScriptInterface(this), "Android");
        webview.loadUrl("http://192.168.0.5/SomeWeb");
    }

    public void openCamera() {
        Intent cameraIntent = new Intent(
                android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == CAMERA_PIC_REQUEST) {
            Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
            String s = storeAndExit(thumbnail);
            webview.loadUrl("javascript:function ImageData(){ return '" + s
                    + "';}");
            _hasImage = true;
        } else {
            Toast.makeText(Cam2.this, "Picture Not taken", 5000).show();
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    public String storeAndExit(Bitmap data) {
        ByteArrayOutputStream jpeg_data = new ByteArrayOutputStream();
        Bitmap myMap = data;
        try {
            if (myMap.compress(CompressFormat.JPEG, 70, jpeg_data)) {
                byte[] code = jpeg_data.toByteArray();
                byte[] output = org.apache.commons.codec.binary.Base64
                        .encodeBase64(code);
                String js_out = new String(output);
                return js_out;
            }
        } catch (Exception e) {
            Toast.makeText(Cam2.this,
                    "STORE IMAGE EXCEPTION: " + e.getMessage(), 5000).show();
        }
        finish();
        return null;
    }
}

1 个答案:

答案 0 :(得分:0)

我认为你应该在onCreate()mnethod

已创建的网络视图&显示网页。

只有你需要检查这一切。

最诚挚的问候,

〜阿努普