Java 如何在Android中以编程方式打开下载的文件?
使用这段代码,我可以从Android中的URL下载文件并将其保存到SD卡中,有没有任何方法可以通过编程方式打开此文件Java 如何在Android中以编程方式打开下载的文件?,java,android,file,Java,Android,File,使用这段代码,我可以从Android中的URL下载文件并将其保存到SD卡中,有没有任何方法可以通过编程方式打开此文件 @Override protected Void doInBackground(Void... arg0) { DownloadFromUrl(); return null; } public static void DownloadFromUrl() { try { File root = android.os.Environm
@Override
protected Void doInBackground(Void... arg0) {
DownloadFromUrl();
return null;
}
public static void DownloadFromUrl() {
try {
File root = android.os.Environment.getExternalStorageDirectory();
File dir = new File (root.getAbsolutePath() + "/SDCard");
if(dir.exists()==false) {
dir.mkdirs();
}
URL url = new URL(DownloadUrl); //you can write here any link
File file = new File(dir, fileName);
long startTime = System.currentTimeMillis();
Log.d("DownloadManager", "download begining");
Log.d("DownloadManager", "download url:" + url);
Log.d("DownloadManager", "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(5000);
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.flush();
fos.close();
Log.d("DownloadManager", "download ready in" + ((System.currentTimeMillis() - startTime) / 1000) + " sec");
} catch (IOException e) {
Log.d("DownloadManager", "Error: " + e);
}
}
}
意图能在这方面有所帮助吗
private static class Task extends AsyncTask<Void, Void, Void> {
static String DownloadUrl = "http://00.00.00.00/abc.crt";
static String fileName = "def.crt";
@Override
protected Void doInBackground(Void... arg0) {
DownloadFromUrl();
return null;
}
public static void DownloadFromUrl() {
try {
File root = android.os.Environment.getExternalStorageDirectory();
File dir = new File (root.getAbsolutePath() + "/SDCard");
if(dir.exists()==false) {
dir.mkdirs();
}
URL url = new URL(DownloadUrl); //you can write here any link
File file = new File(dir, fileName);
long startTime = System.currentTimeMillis();
Log.d("DownloadManager", "download begining");
Log.d("DownloadManager", "download url:" + url);
Log.d("DownloadManager", "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(5000);
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.flush();
fos.close();
Log.d("DownloadManager", "download ready in" + ((System.currentTimeMillis() - startTime) / 1000) + " sec");
} catch (IOException e) {
Log.d("DownloadManager", "Error: " + e);
}
}
}
使用FileInputStream并将下载的文件加载到内存中
@Override
protected Void doInBackground(Void... arg0) {
DownloadFromUrl();
return null;
}
public static void DownloadFromUrl() {
try {
File root = android.os.Environment.getExternalStorageDirectory();
File dir = new File (root.getAbsolutePath() + "/SDCard");
if(dir.exists()==false) {
dir.mkdirs();
}
URL url = new URL(DownloadUrl); //you can write here any link
File file = new File(dir, fileName);
long startTime = System.currentTimeMillis();
Log.d("DownloadManager", "download begining");
Log.d("DownloadManager", "download url:" + url);
Log.d("DownloadManager", "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(5000);
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.flush();
fos.close();
Log.d("DownloadManager", "download ready in" + ((System.currentTimeMillis() - startTime) / 1000) + " sec");
} catch (IOException e) {
Log.d("DownloadManager", "Error: " + e);
}
}
}
如果文件具有特定扩展名,则可能有其他加载方式,而不需要将其解析为字节数组如何打开?作为文本、二进制或应用程序?是的,
意图
是一种方法;看看谷歌或谷歌。当你说“开放”时,你是什么意思?文件类型是什么(如What's.crt)?数据是什么?一串音频?感谢Udrian&Matt,理想情况下。CRT是VPN证书文件,安装后会弹出通知,输入证书名称,然后为Android VPN应用程序安装文件。我想如果我们想读取一个文件,那么我们需要FileInputStream来打开这个文件,不应该使用它吗?GJordis它将作为二进制文件打开,当openedI不知何故错过了一个.CRT文件时,它将调用VPN应用程序。是的,如果您的意图是提示用户在应用程序之外打开此文件,那么您应该使用意图。这可能会对您有所帮助,谢谢Udrian,我尝试了同样的方法,但由于我使用的是异步任务,因此它不允许我在那里调用intent<代码>文件videoFile2Play=新文件(“/sdcard/nice_movie.mpeg”);意图i=新意图();i、 setAction(android.content.Intent.ACTION\u视图);i、 setDataAndType(Uri.fromFile(videoFile2Play),“video/mpeg”);星触觉(i)你试过在主线程上调用它吗?
@Override
protected Void doInBackground(Void... arg0) {
DownloadFromUrl();
return null;
}
public static void DownloadFromUrl() {
try {
File root = android.os.Environment.getExternalStorageDirectory();
File dir = new File (root.getAbsolutePath() + "/SDCard");
if(dir.exists()==false) {
dir.mkdirs();
}
URL url = new URL(DownloadUrl); //you can write here any link
File file = new File(dir, fileName);
long startTime = System.currentTimeMillis();
Log.d("DownloadManager", "download begining");
Log.d("DownloadManager", "download url:" + url);
Log.d("DownloadManager", "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(5000);
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.flush();
fos.close();
Log.d("DownloadManager", "download ready in" + ((System.currentTimeMillis() - startTime) / 1000) + " sec");
} catch (IOException e) {
Log.d("DownloadManager", "Error: " + e);
}
}
}