开发者

Save WebView on Load of another Activity?

I have a webview and i load it with an external URL so this loads the First page and i click on a link goes to the second page then i click on the third page .

Now on this third page i have written a Javascript to open Another Activity which works fine . But on close of this Activity my WebView reloads the First page again which should had rather been same page where the Activity was called ..??

Any idea as to What is it that i am doing wrong ?

Thanks in Advance.

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 = (开发者_JAVA技巧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;
    }
}


I think you should in the onCreate() mnethod

that web view already created & displays the web pages.

only you need to check this thats all.

Best Regards,

~Anup

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜