[英]How to read pdf file from raw folder?
I want to read the pdf file from raw folder if devices have any pdfreader.. Here is my code: 如果设备具有任何pdfreader,我想从原始文件夹中读取pdf文件。这是我的代码:
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("android.resource://com.powergroupbd.pdfreader/raw" + R.raw.androidtasksmwp));
intent.setType("application/pdf");
PackageManager pm = getPackageManager();
List<ResolveInfo> activities = pm.queryIntentActivities(intent, 0);
if (activities.size() > 0) {
startActivity(intent);
} else {
Toast.makeText(getApplicationContext(), "No pdfreader found", Toast.LENGTH_LONG).show();
}
But when i run this project it shows error.here is the logcat result: 但是当我运行这个项目时,它显示error。这是logcat结果:
06-21 02:05:04.408: W/dalvikvm(7763): threadid=1: thread exiting with uncaught exception (group=0x40015560)
06-21 02:05:04.418: E/AndroidRuntime(7763): FATAL EXCEPTION: main
06-21 02:05:04.418: E/AndroidRuntime(7763): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.qo.android.htcgep/com.qo.android.am.pdflib.app.RenderScreen}: java.lang.NullPointerException
06-21 02:05:04.418: E/AndroidRuntime(7763): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
06-21 02:05:04.418: E/AndroidRuntime(7763): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
06-21 02:05:04.418: E/AndroidRuntime(7763): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
06-21 02:05:04.418: E/AndroidRuntime(7763): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
06-21 02:05:04.418: E/AndroidRuntime(7763): at android.os.Handler.dispatchMessage(Handler.java:99)
06-21 02:05:04.418: E/AndroidRuntime(7763): at android.os.Looper.loop(Looper.java:130)
06-21 02:05:04.418: E/AndroidRuntime(7763): at android.app.ActivityThread.main(ActivityThread.java:3683)
06-21 02:05:04.418: E/AndroidRuntime(7763): at java.lang.reflect.Method.invokeNative(Native Method)
06-21 02:05:04.418: E/AndroidRuntime(7763): at java.lang.reflect.Method.invoke(Method.java:507)
06-21 02:05:04.418: E/AndroidRuntime(7763): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-21 02:05:04.418: E/AndroidRuntime(7763): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-21 02:05:04.418: E/AndroidRuntime(7763): at dalvik.system.NativeStart.main(Native Method)
06-21 02:05:04.418: E/AndroidRuntime(7763): Caused by: java.lang.NullPointerException
06-21 02:05:04.418: E/AndroidRuntime(7763): at com.qo.android.am.pdflib.app.RenderScreen.onNewIntent(Unknown Source)
06-21 02:05:04.418: E/AndroidRuntime(7763): at com.qo.android.am.pdflib.app.RenderScreen.onCreate(Unknown Source)
06-21 02:05:04.418: E/AndroidRuntime(7763): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-21 02:05:04.418: E/AndroidRuntime(7763): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
06-21 02:05:04.418: E/AndroidRuntime(7763): ... 11 more
06-21 02:05:04.418: W/ActivityManager(96): Force finishing activity com.qo.android.htcgep/com.qo.android.am.pdflib.app.RenderScreen
What i am doing wrong? 我做错了什么? please help..:(
请帮忙..:(
I have never tried to do it the way you do, but I use raw resources quite successfully via following procedure: 我从未尝试按照您的方式来做,但是我通过以下过程非常成功地使用了原始资源:
fileResourceStream = Activity.getResources().openRawResource(R.raw.androidclient));//R file references the resource id for you.
fileResourceStream = Activity.getResources()。openRawResource(R.raw.androidclient)); // R文件为您引用资源ID。 In your case it will be R.raw.androidtasksmwp.
在您的情况下,它将是R.raw.androidtasksmwp。
Using this InputStream I copy (usually once the application is installed) the resource file to either device memory or SD card (decision is yours). 使用此InputStream,我将资源文件复制(通常在安装了应用程序之后)到设备内存或SD卡(由您决定)。 Something like this can be devised:
可以这样设计:
BufferedInputStream bIS = new BufferedInputStream( fileResourceStream); try { BufferedOutputStream bOS = new BufferedOutputStream( new FileOutputStream(Globals.applicationDirPath + Globals.exeFileName, false)); int BUFFER_SIZE = 4096; byte[] buff = new byte[BUFFER_SIZE]; int bytesRead = bIS.read(buff, 0, BUFFER_SIZE); while (bytesRead >= 0) { bOS.write(buff, 0, bytesRead); bytesRead = bIS.read(buff, 0, BUFFER_SIZE); } bIS.close(); bOS.flush(); bOS.close(); }// try { catch (Exception e) { Globals.log("Exception in checkAndCopyClientToMemory." + e.toString()); }
Open copied file in any application/for any purpose as file. 在任何应用程序中/出于任何目的打开复制的文件作为文件。
I think you may be missing a "/" after ".../raw" so the uri is incorrect. 我认为您可能在“ ... / raw”之后缺少“ /”,因此uri不正确。
Uri.parse("android.resource://com.powergroupbd.pdfreader/raw" + R.raw.androidtasksmwp)
However I'm not sure you can access resources from an external package like this you may need to copy it to the sdcard then open it with an intent 但是我不确定您是否可以从这样的外部程序包访问资源,您可能需要将其复制到sdcard,然后有意地将其打开
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.