我需要從Android下面的URL下載PDF。任何想法如何可以做到這一點:從Android中的鏈接下載PDF
http://bkinfo.in/Murli/1305/EME-26-05-2013.pdf
同樣,還有一個MP3: http://bkinfo.in/Murli/1305/26-05-2013.mp3
欣賞的想法..
最後.... 以下是完整的代碼我用過的。有人可能是有用的.. 添加這些體現:
確保您的AVD可以寫入SD卡(如果你正在寫卡)。 U可以通過將內存塊分配給AVD Manager中的SDCard進行設置。
public class MainActivity extends Activity {
static ProgressDialog pd;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pd = new ProgressDialog(this);
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd.setCancelable(false);
AsyncTaskTest at = new AsyncTaskTest();
at.execute();
}
public class AsyncTaskTest extends AsyncTask<Void, Integer, Integer> {
Session s = null;
protected void onPreExecute(){
pd.show();
}
protected Integer doInBackground(Void... vd){
try{
String[] urls = new String[3];
urls[0] = "http://bkinfo.in/Murli/1305/HMS-25-05-2013.pdf";
urls[1] = "http://bkinfo.in/Murli/1305/EME-25-05-2013.pdf";
urls[2] = "http://bkinfo.in/Murli/1305/25-05-2013.mp3";
String fileName = urls[2].substring(urls[2].lastIndexOf("/")+1); //Coupying the mp3
URL url = new URL(urls[2]);
URLConnection conection = url.openConnection();
conection.setConnectTimeout(10000);
conection.connect();
int lenghtOfFile = conection.getContentLength();
InputStream input = new BufferedInputStream(url.openStream(),8192);
OutputStream output = new FileOutputStream(Environment.getExternalStorageDirectory()+"/"+fileName);
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1){
total += count;
output.write(data, 0, count);
publishProgress((int) ((total * 100)/lenghtOfFile));
}
output.flush();
output.close();
input.close();
}catch(Exception e){
Log.e("MyError:",e.toString());
}
return 0;
}
protected void onProgressUpdate(Integer... msg) {
pd.setProgress(msg[0]);
}
protected void onPostExecute(Integer in){
pd.dismiss();
showDialog("Done !");
}
private void showDialog(String msg){
final AlertDialog.Builder alertBox = new AlertDialog.Builder(new ContextThemeWrapper(MainActivity.this, android.R.style.Theme_Dialog));
alertBox.setMessage(msg);
alertBox.setCancelable(false)
.setPositiveButton("Ok", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int id){
dialog.cancel();
}
}).show();
}
}
}
可能是相關的:http://stackoverflow.com/questions/4457492/simple-http-example-in-android – mvp