【发布时间】:2015-11-09 08:19:12
【问题描述】:
我一直在使用this PDF library,试图获取 PDF 文件所在的路径,但我一直收到错误消息。我在这里错过了什么?
检查我的 Android 设备监视器,我可以在该数据库文件夹中看到我的数据库,该文件夹也位于 assets 文件夹中,但我找不到所有 pdf 文件?请问他们在哪里?看看下面的图片
谢谢朋友,这是我的错误,我刚刚发现我需要在打开文件之前将文件从资产读取到文件目录中。现在正在使用下面的代码工作
` 私人无效 CopyReadAssets(字符串文件名) { AssetManagerassetManager = this.getAssets();
InputStream in = null;
OutputStream out = null;
File file = new File(this.getFilesDir(), fileName);
try
{
in = assetManager.open(fileName);
out = this.openFileOutput(file.getName(), Context.MODE_WORLD_READABLE);
Utility.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(android.content.Intent.ACTION_VIEW);
File dir_file = new File(file.getAbsolutePath());
//<-- Get FileType
String mimeType = Utility.getFileMineType(dir_file);
intent.setDataAndType(Uri.fromFile(file), mimeType); //"file://" + getFilesDir() + "/abc.pdf"),"application/pdf"
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
if(mimeType.equals("application/pdf")){
Log.e(TAG, Uri.fromFile(file).toString()+" -- "+dir_file);
//mPDFView = new StudentPdfViewer();
// mPDFView.display("Symfony.pdf", false);
//handle it as activity
Intent intent1 = new Intent(this, PdfViewer.class);
intent1.putExtra(Config.COURSE_FILE, fileName);
//intent1.putExtra(SqliteDb.COURSE_NAME, listCategory.get(0).getCategory_name());
//intent1.putExtras(mBundle);
startActivity(intent1);
}else{
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
Toast.makeText(this, "Unable to load selected Course Material, Please check the Study Studio installation guide to install the required dependency softwares", Toast.LENGTH_LONG).show();
}
}
}
`
【问题讨论】:
标签: java android file pdf assets