Currently I have a pdf url
, and I would like to simply using the intent to open it, however, it does not work if I put the url
in intent
View document using below method by default application in device. Here url is the docuement url.
val browserIntent = Intent(
Intent.ACTION_VIEW,
Uri.parse(url)
)
(view.context as AppActivity).startActivity(browserIntent)
I see that you are trying this method defining the data type:
Intent intent = new Intent();
intent.setDataAndType(Uri.parse(url), "application/pdf");
startActivity(intent);
but it will cause:
ActivityNotFoundException: No Activity found to handle Intent
Use method without the type definition and it will work perfectly:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
If you use Intent you will be directed to browser all the Time So ,
Use PDFView Android Widget to View Pdf With in an Android application
Assuming that you already have PDF Url or You can Pass the URL to this activity using Intent
Step 1 : Add Dependencies
implementation 'com.github.barteksc:android-pdf-viewer:3.1.0-beta.1'
implementation 'com.github.barteksc:android-pdf-viewer:2.8.2'
Sync the Gradle
Step 2 : Create a Layout [Text View is Optional]
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ViewPdf">
<com.github.barteksc.pdfviewer.PDFView
android:id="@+id/ViewPdf"
android:layout_height="match_parent"
android:layout_width="match_parent"
/>
<TextView
android:id="@+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible"
/>
</RelativeLayout>
Step 3 : Here is the Activity.java files code , Add URL to pdfUrl variable in middle of the code
package com.example.firebase;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.media.Image;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.webkit.WebView;
import android.widget.TextView;
import android.widget.Toast;
import com.github.barteksc.pdfviewer.PDFView;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class ViewPdf extends AppCompatActivity {
private TextView txt; // You can remove if you don't want this
private PDFView pdf;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_pdf);
pdf = findViewById(R.id.ViewPdf);
txt = findViewById(R.id.txt);
//Intent inten = getIntent();
String pdfUrl = "Add PDF url"
//Toast.makeText(this, pdfUrl, Toast.LENGTH_SHORT).show();
try{
new RetrievePdfStream().execute(pdfUrl);
}
catch (Exception e){
Toast.makeText(this, "Failed to load Url :" + e.toString(), Toast.LENGTH_SHORT).show();
}
}
class RetrievePdfStream extends AsyncTask<String, Void, InputStream>{
@Override
protected InputStream doInBackground(String... strings) {
InputStream inputStream = null;
try {
URL url = new URL(strings[0]);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
if (urlConnection.getResponseCode() == 200) {
inputStream = new BufferedInputStream(urlConnection.getInputStream());
}
} catch (IOException e) {
return null;
}
return inputStream;
}
@Override
protected void onPostExecute(InputStream inputStream) {
pdf.fromStream(inputStream).load();
}
}
}
STEP 4 : Don't Forget to change / Give the pdf url in above code to make it work
you can try this. it works for both .pdf and .docx
String pdfUrl = "abc.pdf"; //
String url = "http://docs.google.com/gview?embedded=true&url=" + pdfUrl;
WebView webView = findViewById(R.id.webview_cv_viewer);
webView.getSettings().setSupportZoom(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(url);
The actual error
android.content.ActivityNotFoundException: No Activity found to
handle Intent
{
act=android.intent.action.VIEW
dat=http://oshc.zizsoft.com/oshc_testing.pdf typ=application/pdf
}
This says that:
broadcast
" and Intent to let the system try to open a PDF
filePDF
)You just need a PDF viewer
of some kind.
Solution
Get a PDF reader
app or use @Mahendra's solution.
Download source code from here (Open Pdf from url in Android Programmatically )
MainActivity.java
package com.deepshikha.openpdf;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
public class MainActivity extends AppCompatActivity {
WebView webview;
ProgressBar progressbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webview = (WebView)findViewById(R.id.webview);
progressbar = (ProgressBar) findViewById(R.id.progressbar);
webview.getSettings().setJavaScriptEnabled(true);
String filename ="http://www3.nd.edu/~cpoellab/teaching/cse40816/android_tutorial.pdf";
webview.loadUrl("http://docs.google.com/gview?embedded=true&url=" + filename);
webview.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url){
// do your stuff here
progressbar.setVisibility(View.GONE);
}
});
}
}