文件没有保存在点击

时间:2013-11-02 07:32:01

标签: android file download

设置下载onClick。我没有得到任何错误......文件永远不会被保存。任何人都可以告诉我我错过了什么。把头发拉了2个小时,这可能是一件很小的事情......

以下是代码:

        package com.mynavy;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import java.net.*;

import android.util.*;
import android.os.*;
import java.io.*;

public class YNrank extends Activity {
    Button E4;
    String url1 = "http://doni.daps.dla.mil/SECNAV%20Manuals1/5000.2.pdf";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.rank_select);
        addListenerOnButton();      
    }
    public void addListenerOnButton() 

      { 
              E4 = (Button) findViewById(R.id.E4);
              E4.setOnClickListener(new OnClickListener() 

              {

                public void onClick(View v){
                    new download().execute(url1);

                }

                class download extends AsyncTask<String, Integer, String>{


                    protected String doInBackground(String... url1) {

                    try {

                        URL url = new URL(url1[0]);
                        HttpURLConnection urlConnection = (HttpURLConnection) url
                            .openConnection();

                    String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/download/E4.pdf";

                    InputStream in = urlConnection.getInputStream();
                    OutputStream output = new FileOutputStream(path);
                    int read = 0;
                    byte[] buffer = new byte[32768];
                    while( (read = in.read(buffer)) > 0) {
                      output.write(buffer, 0, read);
                    }
                        output.close();
                        in.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    return null;

                    }
                }
        });
};}

完成代码的一些更新。回答一些问题。 是的我有INTERNET和WRITE_EXTERNAL_STORAGE权限。 拿出吐司。

LOGCAT FOR堆栈输出:

11-03 05:08:13.557: D/dalvikvm(1364): GC_FOR_ALLOC freed 154K, 7% free 3682K/3952K, paused 34ms, total 46ms
11-03 05:08:14.196: W/System.err(1364): java.io.FileNotFoundException: /storage/sdcard/download/E4.pdf: open failed: ENOENT (No such file or directory)
11-03 05:08:14.206: W/System.err(1364):     at libcore.io.IoBridge.open(IoBridge.java:409)
11-03 05:08:14.216: W/System.err(1364):     at java.io.FileOutputStream.<init>(FileOutputStream.java:88)
11-03 05:08:14.226: W/System.err(1364):     at java.io.FileOutputStream.<init>(FileOutputStream.java:128)
11-03 05:08:14.226: W/System.err(1364):     at java.io.FileOutputStream.<init>(FileOutputStream.java:117)
11-03 05:08:14.236: W/System.err(1364):     at com.mynavy.YNrank$1$download.doInBackground(YNrank.java:54)
11-03 05:08:14.236: W/System.err(1364):     at com.mynavy.YNrank$1$download.doInBackground(YNrank.java:1)
11-03 05:08:14.256: W/System.err(1364):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
11-03 05:08:14.256: W/System.err(1364):     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
11-03 05:08:14.266: W/System.err(1364):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
11-03 05:08:14.266: W/System.err(1364):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
11-03 05:08:14.278: W/System.err(1364):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
11-03 05:08:14.278: W/System.err(1364):     at java.lang.Thread.run(Thread.java:841)
11-03 05:08:14.286: W/System.err(1364): Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
11-03 05:08:14.296: W/System.err(1364):     at libcore.io.Posix.open(Native Method)
11-03 05:08:14.306: W/System.err(1364):     at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
11-03 05:08:14.316: W/System.err(1364):     at libcore.io.IoBridge.open(IoBridge.java:393)
11-03 05:08:14.316: W/System.err(1364):     ... 11 more

1 个答案:

答案 0 :(得分:0)

您正在从网络中检索文件,并假设整个文件已经可用..这可能是真的,但可能是错误的。

int read = 0;
byte[] buffer = new byte[32768];
while( (read = input.read(buffer)) > 0) {
  output.write(buffer, 0, read);
}

output.close();
input.close(); 

您还需要WRITE_EXTERNAL_STORAGE权限。

修改

尝试更改

 URL url = new URL(url1[0]);
 URLConnection conexion = url.openConnection();
 conexion.connect();
 InputStream input = new BufferedInputStream(url.openStream());

URL url = new URL(url1[0]);
HttpURLConnection urlConnection = (HttpURLConnection) url
                .openConnection();

InputStream in = urlConnection.getInputStream();

看看它是否有所不同。

此外,应仅从UI线程管理UI。移动这一行

 Toast.makeText(null, "FAIL!", Toast.LENGTH_SHORT).show();

onPostExecute内或使用Handler在UI线程队列中发布Runnable