如何从assets文件夹中打开Android中的PDF文件?

如何从assets文件夹中打开Android中的PDF文件?,第1张

概述有没有人知道如何在 Android中打开PDF文件?我的代码看起来像这样: public class SampleActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); 有没有人知道如何在 Android中打开pdf文件?我的代码看起来像这样:
public class SampleActivity extends Activity {    @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.main);        copyReadAssets();        }    private voID copyReadAssets() {        AssetManager assetManager = getAssets();        inputStream in = null;        OutputStream out = null;        file file = new file(getfilesDir(),"git.pdf");        try {            in = assetManager.open("git.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() + "/git.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,read);        }    }}
解决方法 以下是从资产文件夹打开pdf文件的代码,但您必须在设备上安装pdf阅读器:
private voID copyAssets() {        AssetManager assetManager = getAssets();        inputStream in = null;        OutputStream out = null;        file file = new file(getfilesDir(),"filename.pdf");        try {            in = assetManager.open("filename.pdf");            out = openfileOutput(file.getname(),e.getMessage());        }        Intent intent = new Intent(Intent.ACTION_VIEW);        intent.setDataAndType(                Uri.parse("file://" + getfilesDir() + "/filename.pdf"),read);        }    }
总结

以上是内存溢出为你收集整理的如何从assets文件夹中打开Android中的PDF文件?全部内容,希望文章能够帮你解决如何从assets文件夹中打开Android中的PDF文件?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1139178.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-31
下一篇2022-05-31

发表评论

登录后才能评论

评论列表(0条)

    保存