
但有些东西不能正常工作.我知道字节数组在存储之前和之后与编码String相同,所以这不是问题.
我认为问题是在创建文件以打开意图的过程中的某个地方,但我不确定.
创建字符串:
byte[] b = files.toByteArray(pdf); String encodedfile = Base64.encodetoString(b,Base64.DEFAulT);
pdf是我从中得到的文件:
else if (requestCode == PICK_pdf_REQUEST && resultCode == RESulT_OK && data != null && data.getData() != null) { Uri uri = data.getData(); try { String filename = uri.toString(); filename = filename.substring(filename.length()-10); service.addpdf(order,filename,new file(uri.getPath())); } catch (IOException e) { e.printstacktrace(); } updatefileList(); } 从字符串获取文件:
case pdf: try { byte[] pdfAsBytes = Base64.decode(file.getContent(),Base64.DEFAulT); file dir = getStorageDir(); file pdffile = new file(dir,file.getname()); if(!pdffile.exists()) { pdffile.getParentfile().mkdirs(); pdffile.createNewfile(); } files.write(pdfAsBytes,pdffile); Intent pdfIntent = new Intent(Intent.ACTION_VIEW); pdfIntent.setDataAndType(Uri.fromfile(pdffile),"application/pdf"); pdfIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); startActivity(pdfIntent); } catch (IOException e) { e.printstacktrace(); } break; 此代码运行时没有错误,但pdf查看器无法显示该文件.我试过几个观众.我怀疑生成的文件
解决方法 结果我需要保存到外部存储而不是dir.file dir = getStorageDir();
应该
file dir = Environment.getExternalStorageDirectory();
然后它工作.
总结以上是内存溢出为你收集整理的如何从Android上的String正确解码PDF?全部内容,希望文章能够帮你解决如何从Android上的String正确解码PDF?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)