这是我要下载然后从 sdcard 打开文件的代码。
但是下载文件后,当我点击文件打开它时,它会显示一条 Toast 消息:cannot open file
请告诉我错误在哪里。
package com.pdf;
import java.io.File;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.webkit.WebView;
import android.widget.Toast;
public class PdffileActivity extends Activity
{
WebView webview;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webview = (WebView) findViewById(R.id.webview);
String pdf = "http://officeofthemufti.sg/MaulidBooklet.pdf";
webview.loadUrl("http://docs.google.com/gview?embedded=true&url=" + pdf);
setContentView(webview);
File file=new File("/sdcard/MaulidBooklet.pdf");
if (file.exists())
{
Uri path = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
try
{
startActivity(intent);
}
catch (ActivityNotFoundException e)
{
Toast.makeText(PdffileActivity.this,
"No Application Available to View PDF",
Toast.LENGTH_SHORT).show();
}
}
}