我正在開發像playstore這樣的應用程序,用戶可以在其中下載任何應用程序。我在我的應用程序中有許多應用程序,我通過wp api v2從我的網站獲得了應用程序。當我們點擊任何可用的應用程序細節打開,它有一個下載鏈接。當我們點擊鏈接時,它會進入瀏覽器,但我想要的是當我們點擊任何應用程序下載鏈接下載應該在我的應用程序與進度條開始。我還沒有找到任何適當的解決方案,但在堆棧或任何地方。 Here is the screenshot attached for better understanding. arrow is pointing to the downloading link.點擊網頁鏈接下載應用程序內的文件。
-2
A
回答
0
您可以使用intent服務下載應用程序。
下面是代碼:
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
通過嘗試此代碼這個服務,你可以把這個鏈接(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讓我試試這個 –
相關問題
- 1. 從應用程序加載網頁,然後點擊該頁面上的鏈接?
- 2. 點擊網頁鏈接應用程序將打開
- 3. VBA點擊網頁鏈接
- 4. 加載網頁並單擊從控制檯應用程序下載文件
- 5. 打開文件鏈接與用戶點擊應用程序
- 6. 從我的網站/ Facebook應用程序點擊鏈接統計
- 7. 下載XSLX文件錨點鏈接點擊Laravel 5.2
- 8. 從網頁在WP7應用程序下載文件
- 9. Chrome擴展程序:點擊鏈接時更改鏈接網址
- 10. 外部應用程序 - 網格內的鼠標點擊事件
- 11. 點擊鏈接去不了網頁
- 12. AngularJS:點擊鏈接加載頁面
- 13. Angular2 - Html鏈接文件下載路線後,點擊
- 14. 文件損壞時,點擊下載鏈接
- 15. 網頁流量將不會加載的鏈接點擊
- 16. 點擊網頁中的pdf鏈接,如何預覽而不是下載?
- 17. JavaScript的網頁應用程序的永久鏈接頁面
- 18. Nightwatch測試點擊下載鏈接並驗證csv的內容
- 19. 點擊鏈接進入緩慢加載第三方網頁
- 20. 點擊鏈接後獲取夢魘等待下一頁加載
- 21. 點擊兩個鏈接,點擊一下
- 22. 從鏈接下載文件
- 23. 如何點擊iphone應用程序中的按鈕並鏈接到網站
- 24. UI聊天應用程序中的可點擊鏈接
- 25. 可點擊的外部鏈接inisde facebook畫布應用程序
- 26. 打開我的Android應用程序時點擊.mp3鏈接
- 27. 當我點擊自動鏈接的URL,應用程序崩潰
- 28. 下載鏈接點擊瀏覽器中打開下載窗口
- 29. 啓動窗體應用程序時,瀏覽文件在網頁上點擊
- 30. 從網站上的鏈接下載網站上的jar文件
文件下載只是像其他任何請求一樣。如果你有一個已經調用API的應用程序,你當然可以下載文件。 https://futurestud.io/tutorials/retrofit-2-how-to-download-files-from-server – vlatkozelka
兄弟@vlatkozelka我看着它。他正在手動將網址放入活動中。但我有我的應用程序中的很多鏈接,我必須通過單擊鏈接下載 –
這將是一個完全不同的問題,關於如何在應用程序中傳遞數據。 – vlatkozelka