因为您想在单独的应用程序(例如Adobe Reader)中显示PDF文件,所以我希望执行以下 *** 作:
private void CopyReadAssets() { AssetManager assetManager = getActivity().getAssets(); InputStream in = null; OutputStream out = null; String state = Environment.getExternalStorageState(); if (!Environment.MEDIA_MOUNTED.equals(state)) { Toast.makeText(getActivity(), "External Storage is not Available", Toast.LENGTH_SHORT).show(); } File pdfDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/PDFs"); if (!pdfDir.exists()) { pdfDir.mkdir(); } File file = new File(pdfDir + "/abc.pdf"); try { in = assetManager.open("abc.pdf"); out = new BufferedOutputStream(new FileOutputStream(file)); copyFile(in, out); in.close(); in = null; out.flush(); out.close(); out = null; } catch (Exception e) { Log.e("tag", e.getMessage()); } if (file.exists()) //Checking for the file is exist or not { Uri path = Uri.fromFile(file); Intent objIntent = new Intent(Intent.ACTION_VIEW); objIntent.setDataAndType(path, "application/pdf"); objIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); Intent intent1 = Intent.createChooser(objIntent, "Open PDF with.."); try { startActivity(intent1); } catch (ActivityNotFoundException e) { Toast.makeText(getActivity(), "Activity Not Found Exception ", Toast.LENGTH_SHORT).show(); } } else { Toast.makeText(getActivity(), "The file not exists! ", Toast.LENGTH_SHORT).show(); } }
要将文件复制到设备内存中:
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); } }
使用以下权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)