android中找不到文件错误?

时间:2014-07-01 22:13:41

标签: android

我正在尝试下载pdf文件并在某个位置保存在android上。但是我收到此错误的原因是什么? java.io.FileNotFoundException:http://www.pdf995.com/samples/pdf.pdf 我正在检查我在emulator上的代码。请告诉我为什么会出现这个错误,因为当我在浏览器http://www.pdf995.com/samples/pdf.pdf上点击这个网址时,它会向我们提供pdf。但是这里没有给出使用,

代码:

package com.example.downloadfile;

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

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;



public class MainActivity extends Activity {
    int totalSize = 0;
     int downloadedSize = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button b = (Button) findViewById(R.id.b1);
        b.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                 //showProgress(dwnload_file_path);

                    new Thread(new Runnable() {
                        public void run() {
                             downloadFile();
                        }
                      }).start();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
      void downloadFile(){

            try {
                URL url = new URL("http://www.pdf995.com/samples/pdf.pdf");
                HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

                urlConnection.setRequestMethod("GET");
                urlConnection.setDoOutput(true);

                //connect
                urlConnection.connect();

                //set the path where we want to save the file           
                File SDCardRoot = Environment.getExternalStorageDirectory(); 
                //create a new file, to save the <span id="IL_AD12" class="IL_AD">downloaded</span> file 
                File file = new File(SDCardRoot,"aaa.pdf");

                FileOutputStream fileOutput = new FileOutputStream(file);

                //Stream used for reading the data from the internet
                InputStream inputStream = urlConnection.getInputStream();

                //this is the total size of the file which we are <span id="IL_AD9" class="IL_AD">downloading</span>
                totalSize = urlConnection.getContentLength();



                //create a buffer...
                byte[] buffer = new byte[1024];
                int bufferLength = 0;

                while ( (bufferLength = inputStream.read(buffer)) > 0 ) {
                    fileOutput.write(buffer, 0, bufferLength);
                    downloadedSize += bufferLength;
                    // update the progressbar //
                    runOnUiThread(new Runnable() {
                        public void run() {
                            float per = ((float)downloadedSize/totalSize) * 100;
                            //cur_val.setText("Downloaded " + downloadedSize + "KB / " + totalSize + "KB (" + (int)per + "%)" );
                        }
                    });
                }
                //close the output stream when complete //
                fileOutput.close();
                runOnUiThread(new Runnable() {
                    public void run() {
                        // pb.dismiss(); // if you want close it..
                    }
                });         

            } catch (final MalformedURLException e) {
               // showError("Error : MalformedURLException " + e);        
                e.printStackTrace();
            } catch (final IOException e) {
                //showError("Error : IOException " + e);          
                e.printStackTrace();
            }
            catch (final Exception e) {
                //showError("Error : Please <span id="IL_AD4" class="IL_AD">check your</span> internet connection " + e);
            }       
        }

}

1 个答案:

答案 0 :(得分:0)

稍后调用openConnection()就足以访问输入流。看起来第二次调用connect()是导致问题的原因。只需从代码中删除该部分,它就可以正常工作:

URL url = new URL("http://www.pdf995.com/samples/pdf.pdf");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

//set the path where we want to save the file           
File SDCardRoot = Environment.getExternalStorageDirectory(); 
...

顺便说一句,如果您想在下载过程中显示进度,我建议您改用AsyncTask。它是为这样的用途量身定做的。