Android研究
Java代码
1、Android从网上下载文件
Java代码
URL url=null;
try {
url = new URL("http://192.168.1.100/Test/111.mp3");
HttpURLConnection urlConn = (HttpURLConnection) url
.openConnection();
BufferedInputStream bis = new BufferedInputStream(urlConn
.getInputStream());
FileOutputStream fos = new FileOutputStream(
Environment.getExternalStorageDirectory().getName()+
"\\music\\6.mp3");
BufferedOutputStream bos = new BufferedOutputStream(fos);
byte[] buf = new byte[3 * 1024];
int result = bis.read(buf);
while (result != -1) {
bos.write(buf, 0, result);
result = bis.read(buf);
}
bos.flush();
bis.close();
fos.close();
bos.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Xml代码
<uses-permission android:name="android.permission.INTERNET"/><!--访问网络-->
Xml代码
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/><!--访问sd卡-->
2、android实现多线程
Java代码
package cd.edu.app;
import android.app.Activity;
import android.os.Bundle;
import android.os.HandlerThread;
import android.os.Message;
import cn.edu.subclass.MyHandler;
public class AndroidThreadActivity extends Activity {
相关新闻>>
- 发表评论
-
- 最新评论 更多>>