假设我有Android应用程序,并且在Android应用程序中也有一个功能下载按钮。点击下载按钮,然后下载当前的APK文件。可能吗。如果是,那么请以编程方式帮助我如何做到这一点。
答案 0 :(得分:0)
如果您想在手机中预装apk或安装在手机中
请先尝试查看此内容,但需要root电话才能从system / app /
获取文件Does Android keep the .apk files? if so where?
我认为你可以使用那里的代码将apk复制到你的SD卡
答案 1 :(得分:0)
将apk文件放入特定网址。通过使用文件流,您可以下载.apk文件。可以使用基本下载代码进行下载。
检查以下代码:
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);
}
}
}