从Android中的链接下载pdf

从Android中的链接下载pdf,android,http,Android,Http,我需要从下面的Android URL下载pdf。你知道如何做到这一点吗: 同样,还有一个mp3: 欣赏这些想法 最后。。。。 这是我使用的完整代码。可能对某人有用。。 将这些添加到清单中: 确保您的AVD可以写入SD卡(如果您正在写入SD卡)。您可以通过在AVD管理器中将内存chunck分配给SD卡进行设置 public class MainActivity extends Activity { static ProgressDialog pd;

我需要从下面的Android URL下载pdf。你知道如何做到这一点吗:

同样,还有一个mp3:

欣赏这些想法

最后。。。。 这是我使用的完整代码。可能对某人有用。。 将这些添加到清单中:

确保您的AVD可以写入SD卡(如果您正在写入SD卡)。您可以通过在AVD管理器中将内存chunck分配给SD卡进行设置

    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();
                    }
            }
    }
公共类MainActivity扩展活动{
静电放电;
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pd=新进度对话框(本);
pd.setProgressStyle(ProgressDialog.STYLE_水平);
pd.可设置可取消(假);
AsyncTaskTest at=新建AsyncTaskTest();
at.execute();
}
公共类AsyncTaskTest扩展了AsyncTask{
会话s=null;
受保护的void onPreExecute(){
pd.show();
}
受保护的整数doInBackground(Void…vd){
试一试{
字符串[]URL=新字符串[3];
URL[0]=”http://bkinfo.in/Murli/1305/HMS-25-05-2013.pdf";
URL[1]=”http://bkinfo.in/Murli/1305/EME-25-05-2013.pdf";
URL[2]=”http://bkinfo.in/Murli/1305/25-05-2013.mp3";
字符串文件名=URL[2]。子字符串(URL[2]。lastIndexOf(“/”)+1);//耦合mp3
URL=新的URL(URL[2]);
URLConnection conconnection=url.openConnection();
连接设置连接超时(10000);
conconnect.connect();
int lenghtOfFile=conconnect.getContentLength();
InputStream输入=新的BufferedInputStream(url.openStream(),8192);
OutputStream output=新文件OutputStream(Environment.getExternalStorageDirectory()+“/”+文件名);
字节数据[]=新字节[1024];
长总计=0;
而((计数=输入。读取(数据))!=-1){
总数+=计数;
输出.写入(数据,0,计数);
出版进度((整数)((总计*100)/长度办公室);
}
output.flush();
output.close();
input.close();
}捕获(例外e){
Log.e(“MyError:,e.toString());
}
返回0;
}
受保护的void onProgressUpdate(整数…msg){
pd.setProgress(消息[0]);
}
受保护的void onPostExecute(中的整数){
pd.解散();
showDialog(“完成!”);
}
私有void显示对话框(字符串消息){
final AlertDialog.Builder alertBox=新建AlertDialog.Builder(新建ContextThemeWrapper(MainActivity.this,android.R.style.Theme_对话框));
设置消息(msg);
alertBox.setCancelable(错误)
.setPositiveButton(“确定”,新的DialogInterface.OnClickListener(){
public void onClick(DialogInterface对话框,int-id){
dialog.cancel();
}
}).show();
}
}
}

希望我不是在填鸭!我有一些艰难的时间,所以我决定分享我的工作代码

private void downloadCommandFile(String dlUrl){
    int count;
    try {
        URL url = new URL( dlUrl );
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        con.setDoInput(true);
        con.setDoOutput(true);
        con.connect();
        int fileSize = con.getContentLength();
        Log.d("TAG", "Download file size = " + fileSize );
        InputStream is = url.openStream();
        String dir = Environment.getExternalStorageDirectory() + "dl_directory";
        File file = new File( dir );
        if( !file.exists() ){
            file.mkdir();
        }

        FileOutputStream fos = new FileOutputStream(file + "EME-26-05-2013.pdf");
        byte data[] = new byte[1024];

        while( (count = is.read(data)) != -1 ){
            fos.write(data, 0, count);
        }

        is.close();
        fos.close();


    } catch (Exception e) {
        Log.e("TAG", "DOWNLOAD ERROR = " + e.toString() );
    }

}




public class DownloadTask extends AsyncTask<String, Void, String>{

    @Override
    protected String doInBackground(String... params) {
             String url = "http://bkinfo.in/Murli/1305/EME-26-05-2013.pdf"; // your url here
        downloadCommandFile( url);
        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        // download complete
    }

}
别忘了将其添加到清单文件中:

<uses-permission android:name="android.permission.INTERNET"/>


可能相关:谢谢忍者!我已经完成了!很快我将发布完整的解决方案,以便其他人可以使用相同的..Good@ravitiwari。我对你的问题投了赞成票!
<uses-permission android:name="android.permission.INTERNET"/>