2017-07-19 10 views
-2

私はユーザーが任意のアプリをダウンロードできるプレイストアのようなアプリを開発しています。私は私のウェブサイトからwp api v2を通して得た多くのアプリを自分のアプリケーションに持っています。使用可能なアプリケーションの詳細をクリックすると、ダウンロードリンクが表示されます。私たちがリンクをクリックするとブラウザに行きますが、私が望むのは、ダウンロードしたアプリケーションのどれかをクリックすると、アプリケーションの中で進行状況バーでリンクを開始する必要があります。私はスタックや場所にまだ適切な解決策が見つかりませんでした。 Here is the screenshot attached for better understanding. arrow is pointing to the downloading link.ウェブリンクをクリックして、アプリ内でファイルをダウンロードします。

+0

ファイルのダウンロードは他のファイルのリクエストと同じです。既にAPIを呼び出しているアプリを持っている場合は、もちろんファイルをダウンロードできます。 https://futurestud.io/tutorials/retrofit-2-how-to-download-files-from-server – vlatkozelka

+0

bro @vlatkozelka私はそれを見ました。彼は手動で活動にURLを入れている。しかし、私はそのリンクをクリックしてダウンロードする必要がある私のアプリで多くのリンクを持っている –

+0

それはアプリ内のデータを渡す方法についてはまったく異なる質問になります。 – vlatkozelka

答えて

0

あなたはアプリをダウンロードするためにインテントサービスを使用できます。ここで

コードです:

public class DownloadService extends IntentService { 
File cacheDir; 

public DownloadService() { 
    super("DownloadService"); 
} 

@Override 
public void onCreate() { 
    super.onCreate(); 
    String tmpLocation = 
      Environment.getExternalStorageDirectory().getPath(); 
    cacheDir = new File(tmpLocation); 
    if (!cacheDir.exists()) { 
     cacheDir.mkdirs(); 
    } 
} 

@Override 
protected void onHandleIntent(Intent intent) { 
    String remoteUrl = intent.getExtras().getString("url"); 
    String location; 
    String filename = 
      remoteUrl.substring(
        remoteUrl.lastIndexOf(File.separator) + 1); 
    File tmp = new File(cacheDir.getPath() 
      + File.separator + filename); 
    if (tmp.exists()) { 
     location = tmp.getAbsolutePath(); 

     stopSelf(); 
     return; 
    } 
    try { 
     URL url = new URL(remoteUrl); 
     HttpURLConnection httpCon = 
       (HttpURLConnection) url.openConnection(); 
     if (httpCon.getResponseCode() != 200) 
      throw new Exception("Failed to connect"); 
     InputStream is = httpCon.getInputStream(); 
     ByteArrayOutputStream out = new ByteArrayOutputStream(); 
     byte[] buf = new byte[1024]; 
     int n = 0; 
     while (-1 != (n = is.read(buf))) { 
      out.write(buf, 0, n); 
     } 
     out.close(); 
     is.close(); 
     byte[] response = out.toByteArray(); 
     FileOutputStream fos = new FileOutputStream(tmp); 
     fos.write(response); 
     fos.flush(); 
     fos.close(); 
     is.close(); 
     location = tmp.getAbsolutePath(); 
    } catch (Exception e) { 
     Log.e("Service", "Failed!", e); 
    } 
} 
} 

実行が意図

+0

コードに進捗バーを追加できるようになりました。 – kanduken

+0

ありがとうございます。 –

+0

その中のwriteStreamは何ですか? –

0

に渡されたURLと、このサービスはこのコードを試してみてください、あなたは、リンク(のTextView)



    private static void downloadFile(String url, File outputFile) { 
     try { 
      URL u = new URL(url); 
      URLConnection conn = u.openConnection(); 
      int contentLength = conn.getContentLength(); 

      DataInputStream stream = new DataInputStream(u.openStream()); 

      byte[] buffer = new byte[contentLength]; 
      stream.readFully(buffer); 
      stream.close(); 

      DataOutputStream fos = new DataOutputStream(new FileOutputStream(outputFile)); 
      fos.write(buffer); 
      fos.flush(); 
      fos.close(); 
     } catch(FileNotFoundException e) { 
      return; // swallow a 404 
     } catch (IOException e) { 
      return; // swallow a 404 
     } 
    } 

のクリックでこれを置くことができます
+0

http://www.coderzheaven.com/2012/04/29/download-file-android-device-remote-server-custom-progressbar-showing-progress/これをチェックしてください –

+0

ありがとう@Sonal Bharwaniは私にこれを試してもらいます –

関連する問題