編輯:關於Android編程
在做項目中要進行文件的下載以及調用系統軟件打開,文件是服務端,以下是一些代碼片段:
運用progressbar和http協議下載文件:
如果沒有這個文件的就先進行創建並下載,如果有的話就打開:
private void playMeida() {
file2 = new File(savePAth + / + filename);
if (!file2.exists()) {
new Thread() {
public void run() {
try {
down_file(path, savePAth);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
} else {
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri data = Uri.parse(savePAth + / + filename);
if (type.equals(Document 22)) {
intent.setDataAndType(data, video/*);
} else {
intent.setDataAndType(data, application/*);
}
startActivity(intent);
}
}
public void down_file(String url, String path) throws IOException {
URL myURL = new URL(url);
URLConnection conn = myURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
this.fileSize = conn.getContentLength();
if (this.fileSize <= 0)
throw new RuntimeException(can not know the file`s size );
if (is == null)
throw new RuntimeException(stream is null);
FileOutputStream fos = new FileOutputStream(path + / + filename);
// FileOutputStream fos = new FileOutputStream(path + / + filename);
byte buf[] = new byte[1024];
downLoadFileSize = 0;
sendMsg(0);
do {
// 循環讀取
int numread = is.read(buf);
if (numread == -1) {
break;
}
fos.write(buf, 0, numread);
downLoadFileSize += numread;
sendMsg(1);
} while (true);
sendMsg(2);
try {
is.close();
} catch (Exception ex) {
Log.e(tag, error: + ex.getMessage(), ex);
}
}
這個是控制progressbar的,利用線程的思維去做
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (!Thread.currentThread().isInterrupted()) {
switch (msg.what) {
case 0:
mPbImage.setMax(fileSize);
mPbTv.setMax(fileSize);
case 1:
mPbImage.setProgress(downLoadFileSize);
mPbTv.setProgress(downLoadFileSize);
break;
case 2:
Toast.makeText(getActivity(), download is completed, 1)
.show();
break;
case -1:
String error = msg.getData().getString(error);
Toast.makeText(getActivity(), error, 1).show();
break;
}
}
super.handleMessage(msg);
}
};
注意事項:
1.由於url是服務端傳過來的,所以不會有.doc,.xslx這種的結果,所以還要進行轉換,根據mineType的轉化
for (int i = 0; i < MIME_MapTable.length; i++) {
if (mineType != null
&& mineType.equals(MIME_MapTable[i][1])) {
productTpye = MIME_MapTable[i][0];
}
}
private String[][] MIME_MapTable = {
{ .3gp, video/3gpp },
{ .apk, application/vnd.android.package-archive },
{ .asf, video/x-ms-asf },
{ .avi, video/x-msvideo },
{ .bin, application/octet-stream },
{ .bmp, image/bmp },
{ .c, text/plain },
{ .class, application/octet-stream },
{ .conf, text/plain },
{ .cpp, text/plain },
{ .doc, application/msword },
{ .docx,
application/vnd.openxmlformats-officedocument.wordprocessingml.document },
{ .xls, application/vnd.ms-excel },
{ .xlsx,
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet },
{ .exe, application/octet-stream },
{ .gif, image/gif },
{ .gtar, application/x-gtar },
{ .gz, application/x-gzip },
{ .h, text/plain },
{ .htm, text/html },
{ .html, text/html },
{ .jar, application/java-archive },
{ .java, text/plain },
{ .jpeg, image/jpeg },
{ .jpg, image/jpeg },
{ .js, application/x-javascript },
{ .log, text/plain },
{ .m3u, audio/x-mpegurl },
{ .m4a, audio/mp4a-latm },
{ .m4b, audio/mp4a-latm },
{ .m4p, audio/mp4a-latm },
{ .m4u, video/vnd.mpegurl },
{ .m4v, video/x-m4v },
{ .mov, video/quicktime },
{ .mp2, audio/x-mpeg },
{ .mp3, audio/x-mpeg },
{ .mp4, video/mp4 },
{ .mpc, application/vnd.mpohun.certificate },
{ .mpe, video/mpeg },
{ .mpeg, video/mpeg },
{ .mpg, video/mpeg },
{ .mpg4, video/mp4 },
{ .mpga, audio/mpeg },
{ .msg, application/vnd.ms-outlook },
{ .ogg, audio/ogg },
{ .pdf, application/pdf },
{ .png, image/png },
{ .pps, application/vnd.ms-powerpoint },
{ .ppt, application/vnd.ms-powerpoint },
{ .pptx,
application/vnd.openxmlformats-officedocument.presentationml.presentation },
{ .prop, text/plain }, { .rc, text/plain },
{ .rmvb, audio/x-pn-realaudio }, { .rtf, application/rtf },
{ .sh, text/plain }, { .tar, application/x-tar },
{ .tgz, application/x-compressed }, { .txt, text/plain },
{ .wav, audio/x-wav }, { .wma, audio/x-ms-wma },
{ .wmv, audio/x-ms-wmv },
{ .wps, application/vnd.ms-works }, { .xml, text/plain },
{ .z, application/x-compress },
{ .zip, application/x-zip-compressed }, { , */* } };
得到的productTpye 就是文件的後綴名。如.jpg
Android 通過Intent使用Bundle傳遞對象Android開發中有時需要在應用中或進程間傳遞對象,下面詳細介紹Intent使用Bundle傳遞對象的方法。被傳
首先給出跑馬燈效果圖 中間的色塊是因為視頻轉成GIF造成的失真,自動忽略哈。大家知道,橫向的跑馬燈android自帶的TextView就可以實現,詳情請百度【A
實驗中只需要編寫相應的xml的代碼,java代碼不需要更改,因為我們這裡只是練習android的界面設計。線性布局:線性布局就是將各種控件按照行或者列依次進行排列。其中本
前邊4篇文章,成功將 u-boot2012 移植到了 2440 開發板上,但是它僅僅支持 norflash 啟動並不夠完善,下面我們設法讓它支持兩種啟動方式。首先,我們得