2017-03-28 9 views
2

私は最近自分のアプリでアクティビティを作成しました。今は、ガイドラインを見たいときに.pdfファイルをダウンロードするようにしました。私はこれをボタンに実装したかったのです。どのようにこれを正しく行うにはどのようなアイデア?ボタンをクリックしてファイルをダウンロードする方法(Androidスタジオ)

以下HERESに私のコード:

public class Exhibitor_Registration_Activity extends AppCompatActivity { 

    Button buttonDownload; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_exhibitor_registration_); 

     this.setTitle("Buyer Registration"); 

     Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar); 
     setSupportActionBar(myToolbar); 
     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
     myToolbar.setNavigationIcon(R.drawable.ic_arrow_back_white_24dp); 

     final Button buttonDownload = (Button) findViewById(R.id.buttonDownload); 

     buttonDownload.setOnClickListener(new View.OnClickListener(){ 

      @Override 
      public void onClick(View view) { 
       try { 
        //this is the file you want to download from the remote server 
        String path ="http://www.manilafame.com/website-assets/downloads/exhibitor-application-kit/local/201704/1-Summary-of-Participation-Details-April-2017_MN_002.pdfp"; 
        //this is the name of the local file you will create 
        String targetFileName = null; 
        boolean eof = false; 
        URL u = new URL(path); 
        HttpURLConnection c = (HttpURLConnection) u.openConnection(); 
        c.setRequestMethod("GET"); 
        c.setDoOutput(true); 
        c.connect(); 
        FileOutputStream f = new FileOutputStream(new File("c:\\junk\\"+targetFileName)); 
        InputStream in = c.getInputStream(); 
        byte[] buffer = new byte[1024]; 
        int len1 = 0; 
        while ((len1 = in.read(buffer)) > 0) { 
         f.write(buffer,0, len1); 
        } 
        f.close(); 
       } catch (MalformedURLException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } catch (ProtocolException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } catch (FileNotFoundException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 
} 

私もherehereからソースコードを得ました。

+1

この[リンク](http://www.androhub.com/android-download-files-save/)を確認してください。 –

+0

私はそれを見ます。 – SovietSenpai

+0

User DownloadManager Android API –

答えて

4

... が、これは

はクラスDownloadManagerを作成する手順に従います。Javaの

public class DownloadManager extends AsyncTask<String,String,String>{ 
String downloadlink,fileDestination; 
public static final int ON_INIT=100,ON_ERROR=102,ON_PROGRASS=103,ON_COMPLETED=104,STATUS_DOWNLOADED=1500,STATUS_NOT_YET=1501; 
private onUpdateListener onUpdateListener; 
private String downloadedPath=""; 
private long downloaded=0; 
private File file; 
private String returnData=null; 
private File cacheDownloadFile; 
public DownloadManager(String downloadlink,String fileDestinationPath){ 
    this.downloadlink=downloadlink; 
    this.fileDestination=fileDestinationPath; 
    file=new File(fileDestination, Tools.getFileName(downloadlink)); 
    cacheDownloadFile=new File(AppCostants.CHACHE_PATH+Tools.getFileName(downloadlink)); 
    try { 
     if(cacheDownloadFile.isFile()) 
      downloaded=Tools.getFileSize(cacheDownloadFile); 
     else 
      downloaded=0; 
     Log.d("FILE_DOWNLOAD_TAG_p",downloaded+" <- "+cacheDownloadFile.getAbsolutePath()); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    fireOnUpdate(ON_INIT,"init ..."); 

} 
@Override 
protected String doInBackground(String... params) { 
    try { 
     File dir=new File(fileDestination); 
     File chacheDir=new File(AppCostants.CHACHE_PATH); 
     if(!chacheDir.isDirectory()) 
      chacheDir.mkdirs(); 
     if(!dir.isDirectory()){ 
      dir.mkdirs(); 
     } 

     if(file.exists()) { 
      Log.d("FILE_DOWNLOAD_TAG","File exist return complete"); 
      return "COMPLETED";//file exist 
     } 
     if(!cacheDownloadFile.exists()){ 
      cacheDownloadFile.createNewFile(); 
     } 
     Log.d("FILE_DOWNLOAD_TAG","LINK "+downloadlink); 
     URL url=new URL(downloadlink); 
     HttpURLConnection urlConnection= (HttpURLConnection) url.openConnection(); 
     if(downloaded>0) 
      urlConnection.setRequestProperty("Range","byte="+downloaded); 
     urlConnection.connect(); 
     int status = urlConnection.getResponseCode(); 
     InputStream inputStream=urlConnection.getInputStream(); 
     int totalSize=urlConnection.getContentLength(); 
     if(totalSize<=downloaded){ 
      returnData= "COMPLETED"; 
      publishProgress("File checked "+Tools.getFileName(file.getAbsolutePath())); 
      return returnData; 
     } 
     this.downloadedPath=cacheDownloadFile.getAbsolutePath(); 
     byte[] buffer=new byte[1024]; 
     int bufferLength=0; 
     FileOutputStream fileOutput=new FileOutputStream(cacheDownloadFile); 
     long d=0; 
     long starttime=System.currentTimeMillis(); 
     while ((bufferLength=inputStream.read(buffer))>0){ 
      fileOutput.write(buffer,0,bufferLength); 
      downloaded+=bufferLength; 
      d+=bufferLength; 
      //String l=" "+Tools.getFileName(file.getAbsolutePath())+" ("+Tools.convertMemory(downloaded)+"/"+Tools.convertMemory(totalSize)+")"; 
      String l=" "+Tools.convertMemory(downloaded)+"/"+Tools.convertMemory(totalSize)+" ("+getDownloadSpeed(starttime,d)+")"; 
      publishProgress(l); 
      if(downloaded>=totalSize){ 
       break; 
      } 
     } 
     Log.d("FILE_DOWNLOAD_TAG","DWONLOADED TO "+downloadedPath+" ("+cacheDownloadFile.length()+")"); 
     fileOutput.close(); 
     if(Tools.fileCopy(file,cacheDownloadFile)){ 
      Log.d("FILE_DOWNLOAD_TAG","file Copied, delete cache"); 
      cacheDownloadFile.delete(); 
     } 
     returnData="COMPLETED"; 
    } catch (MalformedURLException e) { 
     returnData=null; 
     e.printStackTrace(); 
     publishProgress(e.toString()); 
     Log.d("###################",e+""); 

    } catch (IOException e) { 
     returnData=null; 
     e.printStackTrace(); 
     publishProgress(e.toString()); 
    } 

    return returnData; 
} 

@Override 
protected void onProgressUpdate(String... values) { 
    super.onProgressUpdate(values); 
    fireOnUpdate(ON_PROGRASS,values[0]); 
} 

@Override 
protected void onPostExecute(String s) { 
    super.onPostExecute(s); 
    if(s!=null){ 
     fireOnUpdate(ON_COMPLETED,downloadedPath); 
    }else{ 
     fireOnUpdate(ON_ERROR,"Download failed"); 
    } 
} 

public interface onUpdateListener{ 
    void onUpdate(int code,String message); 
} 
public void setOnUpdateListener(onUpdateListener onUpdateListener){ 
    this.onUpdateListener=onUpdateListener; 
} 
private void fireOnUpdate(int code,String message){ 
    if(onUpdateListener!=null) 
     onUpdateListener.onUpdate(code,message); 
} 

private String getDownloadSpeed(long starttime,float totalDownloaded) { 
    long elapsedTime = System.currentTimeMillis() - starttime; 
    //byte : 
    float speed=1000f * totalDownloaded/elapsedTime; 
    return convert(speed); 
} 
private String convert(float value){ 
    long kb=1024 
      ,mb=kb*1024 
      ,gb=mb*1024; 

    if(value<kb){ 
     String speed=(value+""); 
     speed=speed.substring(0,speed.indexOf('.')+2); 
     return speed+" B/s"; 
    }else if(value<mb){ 
     value=value/kb; 
     String speed=(value+""); 
     speed=speed.substring(0,speed.indexOf('.')); 
     return (speed)+" KB/s"; 
    }else if(value<gb){ 
     value=(value/mb); 
     String speed=(value+""); 
     speed=speed.substring(0,speed.indexOf('.')); 
     return speed+" MB/s"; 
    } 
    return ""; 
} 
} 
のonClick(中
  • 使用このコードを

      DownloadManager downloadManager =新しいDownloadManager(URL、ファイルパス);

    1. セットイベント

      downloadManager.setOnUpdateListener(new DownloadManager.onUpdateListener() { 
      @Override 
      public void onUpdate(int code, String message) { 
      if (code == DownloadManager.ON_COMPLETED) { 
      
             } 
             if(DownloadManager.ON_PROGRASS==code){} 
      
            } 
           }); 
      
    2. 開始ダウンロード

      downloadManager.execute(); 
      
    3. libに設定することにより

      compile "commons-io:commons-io:+" 
      
    4. Tools.java

      public static long getFileSize(File file) throws IOException { 
      FileOutputStream fileOutputStream=new FileOutputStream(file); 
      fileOutputStream.close(); 
      return file.length(); 
      } 
      public static boolean fileCopy(File dest,File source){ 
      try { 
          FileUtils.copyFile(source,dest); 
          return true; 
      } catch (IOException e) { 
          e.printStackTrace(); 
          return false; 
      } 
      } 
      
  • +0

    紛失や疑いがある場合は、無料でコメントしてください:) –

    +0

    どこで私のURLを入れますか?私はそれを見逃しましたか? – SovietSenpai

    +0

    ダウンロードリンク(ファイルURL)は、コンストラクタの最初の引数です。 新しいDownloadManager( "http://www.example.com/file.pdf"、Environment.getExternalStorageDirectory()。getAbsolutePath()); 最初の文字列:ダウンロードリンク、URL 第2文字列:ファイルパス、ダウンロードしたファイルの場所。 AppCostantsクラスには、フォルダ、URLなどの静的定数が含まれています... ライブラリ、http legacy、commons-io(ファイルコピー) –

    0

    メインスレッドでファイルをダウンロードすることは本当に悪いことです。この

    Thread thread = new Thread(new Runnable() { 
        @Override 
        public void run() { 
         //your downloading here 
        } 
    }); 
    thread.start(); 
    

    に別々のThread 使用することは、より良い、それは `s、まだあまりよくありません。それにはいくつかの問題があります。

    1)ユーザーは、だから、より良いあなたがより少ないコードを書きたい場合は、おそらく不定、プログレスバーで画面をオーバーレイ追加のレイアウトを表示する

    のダウンロードについては何も知りません。その後、ダウンロードが完了したら、レイアウトを隠すだけです。

    runOnUiThreadrunの中のスレッドで使用できます。

    runOnUiThread(new Runnable() { 
        @Override 
        public void run() { 
         //just hide some popup 
         //or do what you want after downloading is finished 
         popupLayout.serVisibility(View.GONE); 
        } 
    }); 
    

    2)ユーザーがアクションを行います場合は、メモリリークを取得し、おそらく活動は、ダウンロードの終了について知ることができません実行中のスレッドで画面orientaion)を変更するなどの活動/フラグメントを(再作成します。

    • あなたがダウンロードし、少なくとも一方で、この画面で、画面の向きをブロックすることができます。

      は、この問題を解決するためのいくつかの方法があります。あなたの場合にはおそらく最も簡単な方法です。

    • foreground serviceでダウンロードできます。その本当に良い
      練習が、サービスについて学ぶ必要があります。
    • あなたは(あなたがドンですべてのスレッドを使用していないが、あなたはrxJavaを学ぶためのいくつかの時間を必要とするrxJava/rxAndroidのようなものを使用することができ、あなたのActivity/Fragment
    • onDestroythread.interrupt() メソッドを呼び出すことによりダウンロードを中断しようとすることができます)

    UPD

    About threads

    Not so bad tutorial about threads in android

    Threadの代わりにAsyncTaskを使用できますが、特に長時間の操作にはスレッドを使用することを強くお勧めします。あなたは、ダウンロードの再開可能、スピードをしたい場合

    +0

    最初に私はスレッドを聞いたことがあります。サンプルと一緒にこれを実装する方法についての良い情報源があれば? – SovietSenpai

    +1

    @SovietSenpaiあなたは非同期作業について何か聞く必要がありました。それ以外の場合は、長いタスクが実行されている間UIがフリーズします。最新の記事を参照してください。 – DEADMC

    関連する問題