
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文件?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)