我正在开发像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