【问题标题】:Download file within the app by clicking on the weblink.通过单击网络链接在应用程序中下载文件。
【发布时间】:2017-12-24 17:17:56
【问题描述】:

我正在开发像 Playstore 这样的应用程序,用户可以在其中下载任何应用程序。我的应用程序中有许多应用程序是通过 wp api v2 从我的网站获得的。当我们点击任何打开的可用应用程序详细信息时,它就会有一个下载链接。当我们单击链接时,它会转到浏览器,但我想要的是当我们单击任何应用程序下载链接时,下载应该在我的应用程序中开始,并带有进度条。我还没有在堆栈或任何地方找到任何合适的解决方案。 Here is the screenshot attached for better understanding. arrow is pointing to the downloading link.

【问题讨论】:

  • 文件下载只是一个请求。如果您的应用程序已经在调用 API,您当然可以下载文件。 futurestud.io/tutorials/…
  • 兄弟@vlatkozelka 我看了。他正在将 url 手动放入活动中。但我的应用中有许多链接,我必须通过点击该链接来下载这些链接
  • 那将是一个完全不同的问题,关于如何在应用程序中传递数据。

标签: android android-download-manager download


【解决方案1】:

您可以使用意图服务来下载应用程序。

代码如下:

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);
    }
}
}

使用 Intent 中传递的 url 运行此服务

【讨论】:

  • 您现在可以在代码中相应地添加进度条
  • 谢谢@kanduken 让我试试这个。
  • 里面的writeStream是什么?
  • @kanduken 里面的 writeStream 是什么
  • 用户字节数组写入文件输出流
【解决方案2】:

试试这个代码,你可以把它放在点击链接(文本视图)

  

    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
      }
    } 

 

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-01
相关资源
最近更新 更多