从资产的文件夹中使用PDFViewer.jar阅读PDF [英] Reading pdf from assets folder using PDFViewer.jar

查看:142
本文介绍了从资产的文件夹中使用PDFViewer.jar阅读PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想读的使用PDF文件存储在资源文件夹 PDFViewer.jar

我试过这种方式,但我收到错误消息,并且它被不幸终止。

任何人都可以帮助我如何实现它。

 公共类MainActivity延伸活动{

@覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);
    CopyReadAssets();

}

私人无效CopyReadAssets(){
    AssetManager assetManager = getAssets();

    在的InputStream = NULL;
    出的OutputStream = NULL;
    档案文件=新的文件(getFilesDir(),ABC.pdf);
    尝试 {
        在= assetManager.open(ABC.pdf);
        OUT = openFileOutput(file.getName(),Context.MODE_WORLD_READABLE);

        的CopyFile(IN,OUT);
        附寄();
        在= NULL;
        了out.flush();
        out.close();
        OUT = NULL;
    }赶上(例外五){
        Log.e(标签,e.getMessage());
    }

    意向意图=新的意图(Intent.ACTION_VIEW);
    intent.setDataAndType(
            Uri.parse(文件://+ getFilesDir()+/ABC.pdf),
            应用程序/ PDF格式);

    startActivity(意向);
}

私人无效的CopyFile(在的InputStream,OutputStream的了)抛出IOException异常{
    byte []的缓冲区=新的字节[1024];
    INT读取;
    而((读= in.read(缓冲))!= -1){
        out.write(缓冲,0,读);
    }
}


}
 

logcat的详细信息:

  11月2号至五日:09:05.896:E / AndroidRuntime(6516):致命异常:主要
 十一月2号至5号:09:05.896:E / AndroidRuntime(6516):java.lang.RuntimeException的:无法启动的活动
   ComponentInfo {com.syntel.pdfreader / com.syntel.pdfreader.MainActivity}:
   android.content.ActivityNotFoundException:无活动来处理意向{
    ACT = android.intent.action.VIEW DAT =文件:///data/data/com.syntel.pdfreader/files/ABC.pdf典型值=应用程序/ PDF}
  十一月2号至5号:09:05.896:E / AndroidRuntime(6516):在

  android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
  十一月2号至5号:09:05.896:E / AndroidRuntime(6516):在
 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
  十一月2号至5号:09:05.896:E / AndroidRuntime(6516):在
  android.app.ActivityThread.access $ 600(ActivityThread.java:141)
 十一月2号至5号:09:05.896:E / AndroidRuntime(6516):在
  android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1234)
  十一月2号至5号:09:05.896:E / AndroidRuntime(6516):在android.os.Handler.dispatchMessage(Handler.java:99)
 十一月2号至5号:09:05.896:E / AndroidRuntime(6516):在android.os.Looper.loop(Looper.java:137)
十一月2号至5号:09:05.896:E / AndroidRuntime(6516):在
  android.app.ActivityThread.main(ActivityThread.java:5041)
十一月2号至5号:09:05.896:E / AndroidRuntime(6516):在java.lang.reflect.Method.invokeNative(本机方法)
十一月2号至5号:09:05.896:E / AndroidRuntime(6516):在java.lang.reflect.Method.invoke(Method.java:511)
十一月2号至5号:09:05.896:E / AndroidRuntime(6516):在
  com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793)
 十一月2号至5号:09:05.896:E / AndroidRuntime(6516):在
  com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
 十一月2号至5号:09:05.896:E / AndroidRuntime(6516):在dalvik.system.NativeStart.main(本机方法)
 十一月2号至5号:09:05.896:E / AndroidRuntime(6516):android.content.ActivityNotFoundException:由没有造成Activit
Ÿ发现处理意向{行动= android.intent.action.VIEW
  DAT =文件:///data/data/com.syntel.pdfreader/files/ABC.pdf典型值=应用程序/ PDF}
十一月2号至5号:09:05.896:E / AndroidRuntime(6516):在
 android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1622)
十一月2号至5号:09:05.896:E / AndroidRuntime(6516):在
android.app.Instrumentation.execStartActivity(Instrumentation.java:1417)
 

解决方案

我创建了我用这个的 Android的PDF阅读器,阅读器库

您可以下载我的演示与图书馆

I want to read pdf file stored in asset folder using PDFViewer.jar,

I tried this way but I am getting error message and it gets unfortunately stopped.

Could anyone help me how to implement it.

public class MainActivity extends Activity {

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

}

private void CopyReadAssets() {
    AssetManager assetManager = getAssets();

    InputStream in = null;
    OutputStream out = null;
    File file = new File(getFilesDir(), "ABC.pdf");
    try {
        in = assetManager.open("ABC.pdf");
        out = openFileOutput(file.getName(), Context.MODE_WORLD_READABLE);

        copyFile(in, out);
        in.close();
        in = null;
        out.flush();
        out.close();
        out = null;
    } catch (Exception e) {
        Log.e("tag", e.getMessage());
    }

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

    startActivity(intent);
}

private void copyFile(InputStream in, OutputStream out) throws IOException {
    byte[] buffer = new byte[1024];
    int read;
    while ((read = in.read(buffer)) != -1) {
        out.write(buffer, 0, read);
    }
}


}

logcat details:

 02-05 11:09:05.896: E/AndroidRuntime(6516): FATAL EXCEPTION: main
 02-05 11:09:05.896: E/AndroidRuntime(6516): java.lang.RuntimeException: Unable to start activity  
   ComponentInfo{com.syntel.pdfreader/com.syntel.pdfreader.MainActivity}: 
   android.content.ActivityNotFoundException:   No Activity found to handle Intent {
    act=android.intent.action.VIEW dat=file:///data/data/com.syntel.pdfreader/files/ABC.pdf typ=application/pdf    }
  02-05 11:09:05.896: E/AndroidRuntime(6516):   at    

  android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
  02-05 11:09:05.896: E/AndroidRuntime(6516):   at  
 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
  02-05 11:09:05.896: E/AndroidRuntime(6516):   at 
  android.app.ActivityThread.access$600(ActivityThread.java:141)
 02-05 11:09:05.896: E/AndroidRuntime(6516):    at 
  android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
  02-05 11:09:05.896: E/AndroidRuntime(6516):   at android.os.Handler.dispatchMessage(Handler.java:99)
 02-05 11:09:05.896: E/AndroidRuntime(6516):    at android.os.Looper.loop(Looper.java:137)
02-05 11:09:05.896: E/AndroidRuntime(6516):     at 
  android.app.ActivityThread.main(ActivityThread.java:5041)
02-05 11:09:05.896: E/AndroidRuntime(6516):     at java.lang.reflect.Method.invokeNative(Native Method)
02-05 11:09:05.896: E/AndroidRuntime(6516):     at java.lang.reflect.Method.invoke(Method.java:511)
02-05 11:09:05.896: E/AndroidRuntime(6516):     at 
  com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
 02-05 11:09:05.896: E/AndroidRuntime(6516):    at 
  com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
 02-05 11:09:05.896: E/AndroidRuntime(6516):    at dalvik.system.NativeStart.main(Native Method)
 02-05 11:09:05.896: E/AndroidRuntime(6516): Caused by: android.content.ActivityNotFoundException: No Activit
y found to handle Intent { act=android.intent.action.VIEW 
  dat=file:///data/data/com.syntel.pdfreader/files/ABC.pdf typ=application/pdf }
02-05 11:09:05.896: E/AndroidRuntime(6516):     at  
 android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1622)
02-05 11:09:05.896: E/AndroidRuntime(6516):     at 
android.app.Instrumentation.execStartActivity(Instrumentation.java:1417)

解决方案

I have created my live demo using this Android-Pdf-Viewer-Library

You can download my Demo with Library

这篇关于从资产的文件夹中使用PDFViewer.jar阅读PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆