Sachin Malik Sachin Malik - 18 days ago 4
Android Question

How to display documents from URL without external app?

In my app I am getting URLs of the documents, and I want to display them in my app, not in any external app.

This is the code I have used.

public class Doc_webview extends Activity implements AdvancedWebView.Listener{
private AdvancedWebView mWebView;
String tag="doc";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_doc_webview);
mWebView = (AdvancedWebView) findViewById(R.id.webview);
//mWebView.setListener(this, this);
Intent intent=getIntent();
Log.e(tag,"url "+Uri.parse(intent.getStringExtra("URL")).toString());
mWebView.setListener(this, this);
mWebView.loadUrl("http://docs.google.com/gview?embedded=true&url="+Uri.parse(intent.getStringExtra("URL")).toString());

}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
mWebView.onActivityResult(requestCode, resultCode, intent);
// ...
}

@Override
public void onPageStarted(String url, Bitmap favicon) {
Log.e(tag,"page started "+url);
}

@Override
public void onPageFinished(String url) {
Log.e(tag,"page finished "+url);
}

@Override
public void onPageError(int errorCode, String description, String failingUrl) {
Log.e(tag,"page error "+description);
}

@Override
public void onDownloadRequested(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
Log.e(tag,"download started "+url);
}

@Override
public void onExternalPageRequest(String url) {
Log.e(tag,"page request "+url);
}
}


I only want to display them, not open them for editing.

Answer

Well I am using this code to open a document in my webview

    public class ToSite extends Activity {

    WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_view);

        Utils.setShared(this);
        String url = getIntent().getExtras().getString("url");

        System.out.println(url);

        if (!URLUtil.isValidUrl(url)) {
            finish();
            Toast.makeText(this, "URL not Valid", Toast.LENGTH_LONG).show();
        }
        webView = (WebView) findViewById(R.id.webview);
        webView.setWebViewClient(new MyBrowser());

        webView.getSettings().setLoadsImagesAutomatically(true);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        webView.loadUrl(url);
    }

    private class MyBrowser extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }
}

and my url is

http://docs.google.com/gview?embedded=true&url=http://52.24.60.37/guilt/uploads/termcondition/profile60252550.pdf

Google Doc URL : http://docs.google.com/gview?embedded=true&url=\

My Doc URL : http://52.24.60.37/guilt/uploads/termcondition/profile60252550.pdf

appending both the URL to open the Document in WebView