有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Android:下载文件的应用程序不工作(只工作一次)

我正在制作一个将文件下载到内部存储器的应用程序。尽管收到了正确的信息,但现在这些文件不再显示,这还是第一次起作用。有什么问题吗

我在emulator和我的手机(kitkat)上都试过

这是我的密码

new DownloadJSON().execute();

        ListView lv = getListView();

        lv.setOnItemClickListener(new 安卓.widget.AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> arg0, View view, int arg2, long arg3) {

                String memo_filename = ((TextView) view.findViewById(R.id.txt_service)).getText().toString();
                download_path = "http://www.sunshine-ems.balay-indang.com/attachments/memo/"+memo_filename+".docx";
                final String sd_path = getFilesDir().getAbsolutePath();
                //final String sd_path =  Environment.getExternalStorageDirectory().getAbsolutePath();
                dest_file_path = sd_path+"/Download/"+memo_filename+".docx";
                  dialog = ProgressDialog.show(Memo.this, "", "Downloading file...", true);
                     new Thread(new Runnable() {
                            public void run() { 
                                 //Log.d("Download Path", sd_path+""+dest_file_path+" "+download_path);
                                 downloadFile(download_path, dest_file_path);

                            }
                          }).start();    
            }
        });
    }

    //download file
    public void downloadFile(String download_path, String dest_file_path) {
        try {
            URL u = new URL(download_path);
            HttpURLConnection conn = (HttpURLConnection) u.openConnection();
            InputStream stream = conn.getInputStream();

            File f = new File(dest_file_path);
            FileOutputStream fos = new FileOutputStream(f);
            int bytesRead = 0;
            byte[] buffer = new byte[4096];
            while ((bytesRead = stream.read(buffer)) != -1) {
                fos.write(buffer, 0, bytesRead);
            }
            fos.close();
            stream.close();
            hideProgressIndicator();
        } catch(FileNotFoundException e) {
            hideProgressIndicator();
            return; 
        } catch (IOException e) {
            hideProgressIndicator();
            return; 
        }

  }

  void hideProgressIndicator(){
      runOnUiThread(new Runnable() {
          public void run() {
              dialog.dismiss();
              Toast.makeText(Memo.this,"Download Complete. Check SD Card", Toast.LENGTH_LONG).show();
          }
      });  
  }

共 (0) 个答案