Android的:如何下载文件在android系统? [英] Android: How to download file in android?

查看:142
本文介绍了Android的:如何下载文件在android系统?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从URL下载文件。我有以下的code。

 包com.example.downloadfile;进口java.io.BufferedOutputStream;
进口的java.io.File;
进口java.io.FileNotFoundException;
进口java.io.FileOutputStream中;
进口java.io.IOException异常;
进口的java.io.InputStream;
进口java.net.HttpURLConnection中;
进口java.net.MalformedURLException;
进口java.net.ProtocolException;
进口的java.net.URL;进口android.app.Activity;
进口android.os.Bundle;
进口android.os.Environment;
进口android.util.Log;
进口android.widget.TextView;
进口android.widget.Toast;公共类DownloadFile延伸活动{     私有静态字符串文件名=al.jpg;     @覆盖
     公共无效的onCreate(捆绑savedInstanceState)
     {
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
        TextView的电视=新的TextView(本);
        tv.setText(这是下载文件的程序...);        尝试{
            //这是你想从远程服务器下载文件
            字符串路径=htt​​p://www.fullissue.com/wp-content/uploads/2010/12/Adam-Lambert.jpg;
            //这是本地文件,您将创建的名​​称            字符串targetFileName =al.jpg;            布尔EOF = ​​FALSE;            URL U =新的URL(路径);
            HttpURLConnection的C =(HttpURLConnection类)u.openConnection();
            c.setRequestMethod(GET);
            c.setDoOutput(真);
            c.connect();            字符串PATH_op = Environment.getExternalStorageDirectory()+/下载/+ targetFileName;            tv.append(\\ n路径>中+ PATH_op);            FileOutputStream中F =新的FileOutputStream(新文件(PATH_op));            在的InputStream = c.getInputStream();
            字节[]缓冲区=新的字节[1024];
            INT LEN1 = 0;
            而((LEN1 = in.read(缓冲液))大于0){
                f.write(缓冲液,0,LEN1);
            }            f.close();            }赶上(MalformedURLException的E){
            // TODO自动生成catch块
            e.printStackTrace();
            }赶上(的ProtocolException E){
            // TODO自动生成catch块
            e.printStackTrace();
            }赶上(FileNotFoundException异常五){
            // TODO自动生成catch块
            e.printStackTrace();
            }赶上(IOException异常五){
            // TODO自动生成catch块
            e.printStackTrace();
            Toast.makeText(这一点,e.getMessage(),Toast.LENGTH_LONG).show();
        }        tv.append(\\ nAnother追加!);
        this.setContentView(电视);
    }}

有人能告诉我有什么不对的code。我不能看到我应该下载该文件。我是新来的Java和Android开发人员,非常感谢任何帮助。 :)


解决方案

  1. 您的主应用程序线程做网络I / O。在最好的情况,而下载是怎么回事,这将冻结您的UI。在最坏的情况,你的活动将与应用程序没有响应(ANR)对话框崩溃。


  2. 您正在试图写在外部存储目录(下载/ ),它可能不存在。请首先创建目录。


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


此外,请确保您有 WRITE_EXTERNAL_STORAG​​E 许可,您使用亚行logcat ,DDMS,或Eclipse中的DDMS角度来考察LogCat中,寻找你尝试登录的错误信息。

I'm trying to download a file from a URL. I have the following code.

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);
    }

}

Can someone tell me what's wrong with this code. I'm not able to see the file I'm supposed to download. I'm new to java and android dev, many thanks for any help. :)

解决方案

  1. You are doing network I/O on the main application thread. At best, this will freeze your UI while the download is going on. At worst, you activity will crash with an "Application Not Responding" (ANR) dialog.

  2. You are trying to write to a directory (download/) on external storage that might not exist. Please create the directory first.

  3. Please consider switching to using Log.e() for your error logging, as I do not know if printStackTrace() works on Android.

Also, make sure that you have the WRITE_EXTERNAL_STORAGE permission and that you are using adb logcat, DDMS, or the DDMS perspective in Eclipse to examine LogCat and look for the error messages you are trying to log.

这篇关于Android的:如何下载文件在android系统?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆