简体   繁体   中英

How to read a pdf file stored in sdcard using mupdf reader stored in android device

Hi i am working on making an app in android that reads pdf from some given url and then store it in sdcard and then open it with mupdf reader that is in my android device.

I am getting the following error in my logcat :

logcat :

 02-15 05:47:10.276: E/AndroidRuntime(3134): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ohile.openpdf/com.ohile.openpdf.MainActivity}: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=file:///mnt/sdcard/pdf/Read.pdf typ=application/pdf }
        02-15 05:47:10.276: E/AndroidRuntime(3134):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
        02-15 05:47:10.276: E/AndroidRuntime(3134):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
        02-15 05:47:10.276: E/AndroidRuntime(3134):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
        02-15 05:47:10.276: E/AndroidRuntime(3134):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
        02-15 05:47:10.276: E/AndroidRuntime(3134):     at android.os.Handler.dispatchMessage(Handler.java:99)
        02-15 05:47:10.276: E/AndroidRuntime(3134):     at android.os.Looper.loop(Looper.java:137)
        02-15 05:47:10.276: E/AndroidRuntime(3134):     at android.app.ActivityThread.main(ActivityThread.java:5039)
        02-15 05:47:10.276: E/AndroidRuntime(3134):     at java.lang.reflect.Method.invokeNative(Native Method)
        02-15 05:47:10.276: E/AndroidRuntime(3134):     at java.lang.reflect.Method.invoke(Method.java:511)
        02-15 05:47:10.276: E/AndroidRuntime(3134):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
        02-15 05:47:10.276: E/AndroidRuntime(3134):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
        02-15 05:47:10.276: E/AndroidRuntime(3134):     at dalvik.system.NativeStart.main(Native Method)
        02-15 05:47:10.276: E/AndroidRuntime(3134): Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=file:///mnt/sdcard/pdf/Read.pdf typ=application/pdf }
        02-15 05:47:10.276: E/AndroidRuntime(3134):     at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1622)
        02-15 05:47:10.276: E/AndroidRuntime(3134):     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1417)
        02-15 05:47:10.276: E/AndroidRuntime(3134):     at android.app.Activity.startActivityForResult(Activity.java:3370)
        02-15 05:47:10.276: E/AndroidRuntime(3134):     at android.app.Activity.startActivityForResult(Activity.java:3331)
        02-15 05:47:10.276: E/AndroidRuntime(3134):     at android.app.Activity.startActivity(Activity.java:3566)
        02-15 05:47:10.276: E/AndroidRuntime(3134):     at android.app.Activity.startActivity(Activity.java:3534)
        02-15 05:47:10.276: E/AndroidRuntime(3134):     at com.ohile.openpdf.MainActivity.showPdf(MainActivity.java:46)
        02-15 05:47:10.276: E/AndroidRuntime(3134):     at com.ohile.openpdf.MainActivity.onCreate(MainActivity.java:32)
        02-15 05:47:10.276: E/AndroidRuntime(3134):     at android.app.Activity.performCreate(Activity.java:5104)
        02-15 05:47:10.276: E/AndroidRuntime(3134):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
        02-15 05:47:10.276: E/AndroidRuntime(3134):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
        02-15 05:47:10.276: E/AndroidRuntime(3134):     ... 11 more

i wrote following java code :

* DownloadFile.java *

public class DownloadFile {
    public static void downloadFile(String fileUrl, File directory)  {
        try {
            FileOutputStream f = new FileOutputStream(directory);
            URL u = new URL(fileUrl);
            HttpURLConnection c = (HttpURLConnection) u.openConnection();
            c.setRequestMethod("GET");
            c.setDoOutput(true);
            c.connect();
            InputStream in = c.getInputStream();
            byte []buffer = new byte[1024];
            int len = 0;
            while((len = in.read(buffer))>0) {
                f.write(buffer,0,len);
            }
            f.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

MainActivity.java

public class MainActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String extStorageDirectory = Environment.getExternalStorageDirectory()
                .toString();
        File folder = new File(extStorageDirectory, "pdf");
        folder.mkdir();
        File file = new File(folder, "Read.pdf");
        try {
            file.createNewFile();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        DownloadFile.downloadFile("http://14.140.41.194/monali/i.pdf",
                file);

        showPdf();
    }

     public void showPdf()
     {
         File file = new File(Environment.getExternalStorageDirectory()+"/pdf/Read.pdf");
         PackageManager packageManager = getPackageManager();
         Intent testIntent = new Intent(Intent.ACTION_VIEW);
         testIntent.setType("application/pdf");
         List list = packageManager.queryIntentActivities(testIntent, PackageManager.MATCH_DEFAULT_ONLY);
         Intent intent = new Intent();
         intent.setAction(Intent.ACTION_VIEW);
         Uri uri = Uri.fromFile(file);
         intent.setDataAndType(uri, "application/pdf");
         startActivity(intent);
     }
}

Can someone help me out where is the error in my code. ???

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(
                Uri.parse("file://" + getFilesDir() + "/example.pdf"),
                "application/pdf");

        startActivity(intent);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM