1
在這裏,我試圖從我的資產文件夾中讀取.pdf文件,錯誤顯示「此文檔無法打開」。我試圖將.pdf文件複製到SD卡上,然後從那裏讀取,但沒有成功。這是代碼。請幫幫我。從項目中的原始文件夾讀取.pdf
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
File fileBrochure = new File("/sdcard/fleetman.pdf");
if (!fileBrochure.exists())
{
CopyAssetsbrochure();
}
/** PDF reader code */
File file = new File("/sdcard/fleetman.pdf");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file),"application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
try
{
getApplicationContext().startActivity(intent);
}
catch (ActivityNotFoundException e)
{
Toast.makeText(MainActivity.this, "NO Pdf Viewer", Toast.LENGTH_SHORT).show();
}
}
//method to write the PDFs file to sd card
private void CopyAssetsbrochure() {
AssetManager assetManager = getAssets();
String[] files = null;
try
{
files = assetManager.list("");
}
catch (IOException e)
{
Log.e("tag", e.getMessage());
}
for(int i=0; i<files.length; i++)
{
String fStr = files[i];
if(fStr.equalsIgnoreCase("fleetman.pdf"))
{
InputStream in = null;
OutputStream out = null;
try
{
in = assetManager.open(files[i]);
out = new FileOutputStream("/sdcard/" + files[i]);
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
break;
}
catch(Exception e)
{
Log.e("tag", e.getMessage());
}
}
}
}
它的工作原理.. Thanku .. :) – 2014-08-28 05:38:23
@gautamjoshi話,請給我給予好評:P – 2014-08-28 05:39:31
我會給你10個UpVotes。但評級問題。給我你的電子郵件ID。如果我在某些問題上遇到問題,請諮詢您..:P;) – 2014-08-28 06:03:05