Android:如何在android下载文件?

Kri*_*ris 6 java url android file download

我正在尝试从URL下载文件.我有以下代码.

package com.example.downloadfile;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

public class DownloadFile extends Activity {

     private static String fileName = "al.jpg";

     @Override
     public void onCreate(Bundle savedInstanceState) 
     {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        TextView tv = new TextView(this);
        tv.setText("This is download file program... ");

        try {
            //this is the file you want to download from the remote server
            String path ="http://www.fullissue.com/wp-content/uploads/2010/12/Adam-Lambert.jpg";
            //this is the name of the local file you will create

            String targetFileName = "al.jpg";

            boolean eof = false;

            URL u = new URL(path);
            HttpURLConnection c = (HttpURLConnection) u.openConnection();
            c.setRequestMethod("GET");
            c.setDoOutput(true);
            c.connect();

            String PATH_op = Environment.getExternalStorageDirectory() + "/download/" + targetFileName;

            tv.append("\nPath > " + PATH_op);

            FileOutputStream f = new FileOutputStream(new File(PATH_op));

            InputStream in = c.getInputStream();
            byte[] buffer = new byte[1024];
            int len1 = 0;
            while ( (len1 = in.read(buffer)) > 0 ) {
                f.write(buffer,0, len1);
            }

            f.close();

            } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            } catch (ProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
        }

        tv.append("\nAnother append!");
        this.setContentView(tv);
    }

}
Run Code Online (Sandbox Code Playgroud)

有人能告诉我这段代码有什么问题.我无法看到我应该下载的文件.我是java和android dev的新手,非常感谢任何帮助.:)

Com*_*are 7

  1. 您正在主应用程序线程上进行网络I/O. 充其量,这将在下载过程中冻结您的UI.在最坏的情况下,您的活动将因"应用程序无响应"(ANR)对话框而崩溃.

  2. 您正在尝试写入download/可能不存在的外部存储上的目录().请先创建目录.

  3. 请考虑切换到使用Log.e()您的错误日志记录,因为我不知道是否printStackTrace()适用于Android.

此外,请确保您具有Eclipse中的WRITE_EXTERNAL_STORAGE权限和正在使用的adb logcatDDMS或DDMS透视图,以检查LogCat并查找您尝试记录的错误消息.