在Android应用程序中打开PDF
首先,我想说,我已经尝试了几种解决方案、方法、建议,并参考了这里的几个链接来打开PDF。你可以说我慢,但我至少试过了。我想在我的Android应用程序中打开PDF。我使用的是Nexus10平板电脑。我不能使用web视图。我想通过我的一个片段中的OnClickListener打开此pdf。我想我最大的问题是我不确定在哪里保存我的PDF。我尝试了res和assets文件夹。许多示例使用/sdcard/-这只是将其保存在我的设备上吗?如果是,在哪里/如何获得路径?我在平板电脑上的adobe reader中保存了一个.pdf文件。我可以访问该路径吗?我使用的是API min 16目标API 19 我已经尝试了很多不同的方法在Android应用程序中打开PDF,android,pdf,fragment,onclicklistener,Android,Pdf,Fragment,Onclicklistener,首先,我想说,我已经尝试了几种解决方案、方法、建议,并参考了这里的几个链接来打开PDF。你可以说我慢,但我至少试过了。我想在我的Android应用程序中打开PDF。我使用的是Nexus10平板电脑。我不能使用web视图。我想通过我的一个片段中的OnClickListener打开此pdf。我想我最大的问题是我不确定在哪里保存我的PDF。我尝试了res和assets文件夹。许多示例使用/sdcard/-这只是将其保存在我的设备上吗?如果是,在哪里/如何获得路径?我在平板电脑上的adobe reade
public void onClick(View v)
{
switch(v.getId())
{
case R.id.bizbro3:
File pdfFile = new File( // I don't know what to put here / where to save pdf. Have tried /sdcard/ , getresrouces, absolutepath, ect.);
if(pdfFile.exists())
{
Uri path = Uri.fromFile(pdfFile);
Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
pdfIntent.setDataAndType(path, "application/pdf");
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try
{
startActivity(pdfIntent);
}
catch(ActivityNotFoundException e)
{
Toast.makeText(v.getContext(), "Something went wrong. Returning to the Main Menu",
Toast.LENGTH_LONG).show();
fragment = new FragmentThree();
fragment.setArguments(args);
FragmentManager frgManager = getFragmentManager();
frgManager.beginTransaction().replace(R.id.content_frame, fragment)
.commit();
}
}
}
首先在清单中声明权限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
代码失败的原因是提取的APK文件夹(资产/包括在内)是应用程序私有的, 无法被外部应用程序查看。事实上,您已经“邀请”了这样一个外部应用程序来查看您的数据 通过发布意图,没有任何区别 您需要将文件复制到非专用位置(通常为SD卡)并 事情将开始运作:
void coptAssetToSdcard() {
InputStream input = getAssets().open("myFile.pdf");
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File (sdCard.getAbsolutePath() + "/myDir/");
dir.mkdirs();
File outFile = new File(dir, "destFile.pdf");
OutputStream out = new FileOutputStream(outFile);
byte[] buffer = new byte[10*1024];
int nBytes;
while ((nBytes = input.read(buffer)) != -1) {
out.write(buffer, 0, nBytes);
}
out.flush();
out.close();
input.close();
}
请记住将此func放置在异步任务或其他内容中
我还看到,您的代码有一个缺陷(catch ActivityNotFoundException)
机箱设备没有pdf查看器,这是正确的做法。不要放下自己。我们有时都会遇到这样的问题。非常感谢您的回复!那么/yourfolder是我平板电脑上文件夹的路径吗?或者如果你有文件的路径,那么将其转换为uri,比如uri myUri=uri.parse(str);然后将其传递给pdfIntent.setDataAndType(uri,“application/pdf”);对
Environment.getExternalStorageDirectory().getAbsolutePath()
将为您提供根目录,然后将其与/yourpath
连接,将从根目录转到/yourpath
下的任何文件夹。
void coptAssetToSdcard() {
InputStream input = getAssets().open("myFile.pdf");
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File (sdCard.getAbsolutePath() + "/myDir/");
dir.mkdirs();
File outFile = new File(dir, "destFile.pdf");
OutputStream out = new FileOutputStream(outFile);
byte[] buffer = new byte[10*1024];
int nBytes;
while ((nBytes = input.read(buffer)) != -1) {
out.write(buffer, 0, nBytes);
}
out.flush();
out.close();
input.close();
}