我是一个新的android,我正在尝试从服务器读取pdf。我发现不同的方式,并尝试了其中的大部分。我尝试使用webview,使用谷歌文档,但没有适合我。我不喜欢使用另一个第三方或插件。在android中执行PDF阅读器时出现错误
我发现这个代码工作完美,但它从assets文件夹中读取。
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_books_view);
CopyReadAssets();
}
private void CopyReadAssets()
{
AssetManager assetManager = getAssets();
InputStream in = null;
OutputStream out = null;
File file = new File(getFilesDir(), "test.pdf");
try
{
in = assetManager.open("test.pdf");
out = openFileOutput(file.getName(), Context.MODE_WORLD_READABLE);
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(Intent.ACTION_VIEW);
intent.setDataAndType(
Uri.parse("file://" + getFilesDir() + "/test.pdf"),
"application/pdf");
startActivity(intent);
}
private void copyFile(InputStream in, OutputStream out) throws IOException
{
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1)
{
out.write(buffer, 0, read);
}
}
我试着将它修改为:
public class TestActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
//Call an AsycTask so you don't lock the main UI thread
new RequestTask().execute();
}//end onCreate
private class RequestTask extends AsyncTask<String, String, String>
{
//Background task
protected String doInBackground(String... uri)
{
//Stuff you do in background goes here
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response;
String responseString = null;
//-------
String fileName="test";
String fileExtension=".pdf";
try
{
URL url = new URL("my url");
HttpURLConnection c = (HttpURLConnection) url.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
String PATH = Environment.getExternalStorageDirectory() + "/mydownload/";
File file = new File(PATH);
file.mkdirs();
File outputFile = new File(file, fileName+fileExtension);
FileOutputStream fos = new FileOutputStream(outputFile);
InputStream is = c.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
while ((len1 = is.read(buffer)) != -1) {
fos.write(buffer, 0, len1);
}
responseString = fos.toString();
fos.flush();
fos.close();
is.close();
}
catch (ClientProtocolException e)
{
//TODO Handle problems..
}
catch (IOException e)
{
//TODO Handle problems..
}
return responseString;
}
@Override
protected void onPostExecute(String result)
{
super.onPostExecute(result);
//Do anything with response..
//Stuff you do after the asych task is done
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(
Uri.parse(result),
"application/pdf");
startActivity(intent);
}
} //end RequestTask class
,但它给了我举杯消息: ((不支持的文件类型))
有人可以帮我请,我花了几乎整整一天的时间试图找出问题。
你能解释一下吗,我应该把这个类放到我的代码中吗,还是应该创建一个新的类?我很抱歉,如果我打扰你愚蠢的问题,但这是我第一次使用AsyncTask ..感谢 – 2014-10-08 09:21:13
或者我应该把它放入CopyReadAssets函数?我是否还需要调用startactivity(intent)?我很困惑:( – 2014-10-08 10:39:16
你必须创建一个新类并调用你想要检索文档的执行方法 – JoaoBiriba 2014-10-08 17:09:11