Используя этот код, я могу загрузить файл с URL-адреса в Android и сохранить его в SDCard, есть ли способ открыть этот файл программно?Как открыть загруженный файл на Android программно?
Может ли Намерение помочь в этом?
private static class Task extends AsyncTask<Void, Void, Void> {
static String DownloadUrl = "http://00.00.00.00/abc.crt";
static String fileName = "def.crt";
Асинхронный Task загрузить
@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);
}
}
}
как его открыть? Как текстовое или двоичное или с приложением? – Gjordis
Да, «намерения» - это путь; см. Google или [это] (http://stackoverflow.com/questions/7009452/how-to-launch-browser-to-open-local-file). – adrianp
Когда вы говорите «открыто», что вы имеете в виду? Что такое filetype (как в том, что такое .crt)? Каковы данные? Строка? Аудио? –