[英]Open PDF in Android App
First I would like to say that I have tried several solutions, methods, suggestions and referred to several links on here to open a PDF. 首先,我想说我已经尝试了几种解决方案,方法,建议,并参考了此处的一些链接以打开PDF。 You can call me slow, but I have at least tried.
您可以叫我慢一点,但我至少已经尝试过了。 I would like to open a PDF in my Android Application.
我想在我的Android应用程序中打开PDF。 I am on a Nexus 10 tablet.
我使用的是Nexus 10平板电脑。 I cannot use a web view.
我无法使用网络视图。 I want to open this pdf via my OnClickListener in one of my fragments.
我想通过我的片段之一的OnClickListener打开此pdf。 I think my biggest problem is I am unsure where to save my PDF.
我认为我最大的问题是我不确定将PDF保存在哪里。 I have tried res and assets folders.
我尝试使用res和资产文件夹。 Many example use /sdcard/ - is that just saving it on my device?
许多示例使用/ sdcard /-是否只是将其保存在我的设备上? If so where / how to get path?
如果是这样,在哪里/如何获得路径? I have saved a .pdf file in adobe reader on my tablet can I access that path?
我已经在平板电脑上的Adobe Reader中保存了一个.pdf文件,我可以访问该路径吗? I am using API min 16 target API 19.
我正在使用API最小16目标API 19。
I have tried many variations of this 我已经尝试了很多变化
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();
}
}
}
first declare permissions in manifest 首先在清单中声明权限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
then try this 然后试试这个
String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/yourfolder";
File file = new File(path,"file.pdf");
The reason your code fails is that the extracted APK folders, assets/ included, are application-private, and cannot be viewed by an external app. 您的代码失败的原因是,提取的APK文件夹(包括资产/包含在内)是应用程序专用的,并且无法由外部应用程序查看。 The fact that you have "invited" such an external app to view your data by issuing an intent, makes no difference.
您已经通过发出意图“邀请”了这样的外部应用程序以查看数据,这一事实没有任何区别。
You will need to copy the file to a non-private location (typically: sdcard) and things will start working: 您将需要将文件复制到非私有位置(通常为sdcard),然后事情就会开始起作用:
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();
}
Remember to place this func in an AsyncTask or something. 记住将这个函数放在AsyncTask之类的东西中。
I also see that your code has a fall through (catch ActivityNotFoundException) for the case device has no pdf viewer, which is the correct thing to do. 我还看到您的代码在设备没有pdf查看器的情况下遇到了(catch ActivityNotFoundException)错误,这是正确的做法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.