首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >如何动态地从当前的android应用程序下载现有的apk文件?

如何动态地从当前的android应用程序下载现有的apk文件?
EN

Stack Overflow用户
提问于 2014-04-20 20:35:28
回答 2查看 582关注 0票数 0

假设我有现有的android应用程序,并且在android应用程序中也有一个功能下载按钮。单击“下载”按钮后,它将下载当前的APK文件。有可能。如果是,那么请帮助我如何通过编程完成这一工作。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-04-20 20:55:59

如果您想在您的手机中预装apk或安装在您的手机中

请先看看这个,但是需要有根的手机才能从system/app/ /获取文件。

Does Android keep the .apk files? if so where?

我认为您可以使用那里的代码将apk复制到您的sd卡上。

票数 0
EN

Stack Overflow用户

发布于 2014-04-20 21:01:42

将apk文件放入特定的url中。通过使用文件流,您可以下载.apk文件。基本的下载代码可以用来下载是必要的。

检查以下代码:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
package com.helloandroid.imagedownloader;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import org.apache.http.util.ByteArrayBuffer;
import android.util.Log;

   public class ImageManager {
   private final String PATH = "/data/data/com.helloandroid.imagedownloader/";  //put the downloaded file here
   public void DownloadFromUrl(String imageURL, String fileName) {  //this is the downloader method

   try {

         URL url = new URL("http://yoursite.com/" + imageURL); //you can write here any link

            File file = new File(fileName);



             long startTime = System.currentTimeMillis();

             Log.d("ImageManager", "download begining");

             Log.d("ImageManager", "download url:" + url);

            Log.d("ImageManager", "downloaded file name:" + fileName);

            /* Open a connection to that URL. */

             URLConnection ucon = url.openConnection();



             /*

              * Define InputStreams to read from the URLConnection.

             */

            InputStream is = ucon.getInputStream();

             BufferedInputStream bis = new BufferedInputStream(is);



             /*

              * Read bytes to the Buffer until there is nothing more to read(-1).

              */

             ByteArrayBuffer baf = new ByteArrayBuffer(50);

             int current = 0;

             while ((current = bis.read()) != -1) {

                     baf.append((byte) current);

             }



             /* Convert the Bytes read to a String. */

            FileOutputStream fos = new FileOutputStream(file);

            fos.write(baf.toByteArray());

             fos.close();

            Log.d("ImageManager", "download ready in"

                             + ((System.currentTimeMillis() - startTime) / 1000)

                             + " sec");



    } catch (IOException e) {

            Log.d("ImageManager", "Error: " + e);

     }



    }

 }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23190860

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文