簡體   English   中英

在Android中找不到文件錯誤?

[英]File not found error in android?

我正在嘗試下載pdf文件並保存在android的某個位置。但是為什么會出現此錯誤? java.io.FileNotFoundException: http : //www.pdf995.com/samples/pdf.pdf我正在模擬器上檢查我的代碼。能否請您告訴我為什么會出現此錯誤,因為當我在瀏覽器http://上擊中該URL時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);
            }       
        }

}

調用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 ,如果您想在下載過程中顯示進度,建議您改用AsyncTask 它是針對此類用途量身定制的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM