1

Android アプリで、Web サーバーから外部ストレージの /Download フォルダーにファイルをダウンロードしようとしています。ダウンロードコードはHandlerThreadサービス内で実行されます。

このサービスは、ファイルのダウンロード以外の機能を実行しています。ダウンロードのコードは次のようになります。

public void downloadFile(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try{
                    URL url = new URL("http://192.168.1.105/download/apkFile.apk");
                    HttpURLConnection connection = (HttpURLConnection)url.openConnection();
                    InputStream inputStream = connection.getInputStream();

                    File file = new File(Environment.getExternalStorageDirectory().getPath()+"/Download/apkFile.apk");
                    FileOutputStream fileOutputStream = new FileOutputStream(file);
                    int bytesRead;
                    byte[] buffer = new byte[4096];
                    while((bytesRead = inputStream.read(buffer)) != -1){
                        fileOutputStream.write( buffer, 0, bytesRead);
                    }
                    fileOutputStream.close();
                    inputStream.close();
                }catch(Exception e){
                    e.printStackTrace();
                }
            }
        }).start();
    }

実行にエラーはありませんが、ファイルはダウンロードされません。提案してください。

4

2 に答える 2

0

電話

connection.setDoInput(true);
connection.connect();

InputStream inputStream = connection.getInputStream();
于 2015-05-07T16:29:30.933 に答える