安卓中使用HTTP从网络下载文件到SD卡程序
来源:互联网 发布:阿里云对象存储价格 编辑:程序博客网 时间:2024/06/07 05:11
程序实现在安卓中下载在PC端tomcat中的文件并将其存放到SD卡当中并显示。程序主要代码:
//HttpDownLoad类,该类提供了通过url得到一个http连接的方法和一个从指定url得到一个图片的方法,程序代码如下
//FileToSDCard类,该类实现将输入流中的文件按照指定的文件名存放到SD卡指定的路径中,如果文件已存在则先执行删除然后新建,程序代码如下:
//HttpDownLoad类,该类提供了通过url得到一个http连接的方法和一个从指定url得到一个图片的方法,程序代码如下
package yingxian.HttpDownLoad;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
public class HttpDownLoad {
protected String urlStr = null;
public HttpDownLoad() {}
public HttpDownLoad(String downUrlStr) {
this.urlStr = downUrlStr;
}
public String getUrlStr() {
return this.urlStr;
}
public void setUrlStr(String url) {
this.urlStr = url;
}
public HttpURLConnection getHttpConn() {
URL myURL = null;
HttpURLConnection httpUrlConn = null;
try {
myURL = new URL(urlStr);
}
catch(MalformedURLException mfex) {
mfex.printStackTrace();
}
try{
httpUrlConn = (HttpURLConnection)myURL.openConnection();
}
catch(IOException ioex) {
ioex.printStackTrace();
}
return httpUrlConn;
}
public Bitmap getImageBitmap() {
Bitmap bitmap = null;
try {
HttpURLConnection urlConn = getHttpConn();
urlConn.setDoInput(true);
urlConn.connect();
InputStream is = urlConn.getInputStream();
bitmap = BitmapFactory.decodeStream(is);
is.close();
}
catch(IOException ex) {
ex.printStackTrace();
}
return bitmap;
}
}
//FileToSDCard类,该类实现将输入流中的文件按照指定的文件名存放到SD卡指定的路径中,如果文件已存在则先执行删除然后新建,程序代码如下:
package yingxian.FileUtils;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import android.os.Environment;
public class FileToSDCard {
protected String fileName = null;
protected String savePath = null;
protected InputStream is = null;
public FileToSDCard() { }
public FileToSDCard(String fileNameTemp,String saveDir,InputStream isTemp) {
this.fileName = fileNameTemp;
this.savePath = getSDCardPath() + File.separator + saveDir + File.separator;
this.is = isTemp;
}
public String getFileName() {
return this.fileName;
}
public void setFileName(String strTemp) {
this.fileName = strTemp;
}
public String getSavePath() {
return this.savePath;
}
public void setSavePath(String saveDir) {
this.savePath = getSDCardPath() + File.separator + saveDir + File.separator;
}
public void setIS(InputStream isTemp) {
this.is = isTemp;
}
public InputStream getIS() {
return this.is;
}
public boolean isSDCardExits() {
if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
return true;
}
else {
return false;
}
}
public boolean isFileExits(String nameStr) {
File file = new File(nameStr);
if(file.exists()) {
return true;
}
else {
return false;
}
}
public String getFileNameFromURL(String URLStr) {
String fileNameStr = URLStr.substring(URLStr.lastIndexOf(File.separator) + 1);
return fileNameStr;
}
public String getSDCardPath() {
String sdCardPath = Environment.getExternalStorageDirectory().getAbsolutePath();
return sdCardPath;
}
/**创建目录文件的函数,当返回值1--文件已存在;0--文件创建成功;-1--文件创建失败*/
public int createDir() {
if(isFileExits(savePath)) {
return 1;
}
else {
File fileDir = new File(savePath);
if(fileDir.mkdir()) {
return 0;
}
else {
return -1;
}
}
}
public boolean deleteFile() {
File file = new File((this.savePath + this.fileName));
return file.delete();
}
public File createNewFile() throws IOException {
if(this.deleteFile()) {
System.out.println("Delete old file...");
return this.createSDFile();
}
else {
return null;
}
}
public File createSDFile() throws IOException {
File file = new File((savePath + fileName));
if(file.createNewFile()) {
System.out.println("Create new file...");
return file;
}else {
return null;
}
}
public File writeToSDFromInput() {
File file = null;
FileOutputStream output = null;
int readCount = 0;
try {
if(!isSDCardExits()) {
System.out.println("SDcard is not exits***");
return null;
}
if(this.createDir() == -1) {
System.out.println("dir create fail***");
return null;
}
if(!isFileExits(savePath + fileName)) {
System.out.println("create file...");
file = createSDFile();
}
else {
System.out.print("create file exits***\n");
file = this.createNewFile();
}
output = new FileOutputStream(file);
byte[] buffer = new byte[1024];
while((readCount = is.read(buffer)) != -1) {
output.write(buffer,0,readCount);
}
output.flush();
}
catch(IOException ioex) {
ioex.printStackTrace();
}
catch(Exception ex) {
ex.printStackTrace();
}
finally {
try {
if(output != null) {
output.close();
}
if(is != null) {
is.close();
}
}
catch(Exception ex) {
ex.printStackTrace();
}
}
return file;
}
}
// MainActivity 中有三个按钮和一个imageView,按钮my_Btn_Update 实现从网络下载图片并直接将其显示而不存到SD卡。按钮my_Btn_Down 将网络上的图片下载后存到SD卡上。按钮my_Btn_Show 实现将SD卡上的图片显示到imageView中。程序代码如下:
注意:在程序manifest中需要添加程序SD卡读写权利和使用网络的权利 // MainActivity 中有三个按钮和一个imageView,按钮my_Btn_Update 实现从网络下载图片并直接将其显示而不存到SD卡。按钮my_Btn_Down 将网络上的图片下载后存到SD卡上。按钮my_Btn_Show 实现将SD卡上的图片显示到imageView中。程序代码如下:
package yingxian.httpdownloadstudy;
import java.io.IOException;
import java.net.HttpURLConnection;
import yingxian.FileUtils.FileToSDCard;
import yingxian.HttpDownLoad.HttpDownLoad;
import android.os.Bundle;
import android.os.StrictMode;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
private ImageView my_IV_Photo = null;
private Button my_Btn_Update = null;
private Button my_Btn_Down = null;
private Button my_Btn_Show = null;
private FileToSDCard writeToSD = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build());
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectLeakedSqlLiteObjects().detectLeakedClosableObjects().penaltyLog().penaltyDeath().build());
this.initComponent();
}
@Override
protected void onStop() {
super.onStop();
MainActivity.this.finish();
}
private void initComponent() {
this.my_IV_Photo = (ImageView)findViewById(R.id.myIV01);
this.my_Btn_Update = (Button)findViewById(R.id.myBtn01);
this.my_Btn_Down = (Button)findViewById(R.id.myBtn02);
this.my_Btn_Show = (Button)findViewById(R.id.myBtn03);
this.my_Btn_Update.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
HttpDownLoad httpDown = new HttpDownLoad("http://192.168.43.105:8080/MP3/jbfc.png");
my_IV_Photo.setImageBitmap(httpDown.getImageBitmap());
}
});
this.my_Btn_Down.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
HttpDownLoad httpDown = new HttpDownLoad("http://192.168.43.105:8080/MP3/sckxc.png");
HttpURLConnection httpURLConn = httpDown.getHttpConn();
httpURLConn.connect();
writeToSD = new FileToSDCard("yxrs.jpg", "testDir", httpURLConn.getInputStream());
System.out.print("filename="+ writeToSD.getFileName() + "savepath="+ writeToSD.getSavePath()+"\n");
writeToSD.writeToSDFromInput();
}
catch(IOException ioex) {
ioex.printStackTrace();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
});
this.my_Btn_Show.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String myJPGPath = "/mnt/local/testDir/yxrs.jpg";
BitmapFactory.Options option = new BitmapFactory.Options();
option.inSampleSize = 1;
Bitmap bm = BitmapFactory.decodeFile(myJPGPath, option);
my_IV_Photo.setImageBitmap(bm);
}
});
}
}
0 0
- 安卓中使用HTTP从网络下载文件到SD卡程序
- android简单实现从网络下载文件到手机sd卡
- 从网络下载apk 到 rom 或者 sd 卡
- Android 从服务器下载文件保存到sd卡
- 如何从服务端下载的文件保存到SD卡
- 下载到SD卡文件
- 使用AsyncHttpClient下载文件到SD卡上
- android学习-下载文件到SD卡
- HTTP网络远程下载文件到本地
- 如何从网络下载文件到本地
- JAVA从网络下载文件到本地
- Android开发九:从网上下载文件并存储到SD卡中
- Android 随手记----从服务器下载文件保存到sd卡
- Android从远程服务器下载文件到本地sd卡中
- Android从网络获取图片,并缓存到SD卡
- 从网络获取图片,并缓存到SD卡
- Android 从网络获取图片保存到SD卡中
- 下载图片(文件)到SD文件
- java使用JNI调用DLL动态库错误
- Activity的四种launchMode
- UI效率
- 数据访问接口功能及含义
- 给定一个数组,按序排列,从数组找出若干个数,使得这若干个数字的和与M最为接近,(背包问题)
- 安卓中使用HTTP从网络下载文件到SD卡程序
- 黑马程序员———用图论解决基础测试题之农夫过河问题
- 浅析C++中的this指针 通过空指针(NULL)可以正确调用一些类的成员函数?
- javascript原生移动云编程7 - 如何调用云数据服务显示实时天气
- nosql理论基础
- ZOJ 3812 We Need Medicine(牡丹江网络赛D题)
- RAC一个节点的数据库无法启动:ORA-00600: internal error code, arguments: [4:kgstmLdiToMicroTs], [1], [], [], [], [
- 菜单的添加
- Java:函数引用