android有进度条的下载图片并且显示图片
最近工作不忙,感觉对progressBar不熟悉,所以决定写一个有进度条的下载文件例子!下面的代码就是我写的下载一个图片,并且把图片显示出来的代码:layout文件布局很简单:
[html]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/bt"
android:text="开始下载文件" />
<ProgressBar
android:id="@+id/pb"
android:layout_width="fill_parent"
style="?android:attr/progressBarStyleHorizontal"
android:layout_height="wrap_content" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/img"/>
</LinearLayout>
一个文件工具类:
[java]
package com.spring.sky.download;
import java.io.File;
import android.content.Context;
import android.os.Environment;
import android.util.Log;
/**
* 文件工具类
* @author spring sky
* Email:vipa1888@163.com
* QQ:840950105
* name:石明政
*
*/
public class FileUtil {
/**
* 检验SDcard状态
* @return boolean
*/
public static boolean checkSDCard()
{
if(android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
{
return true;
}else{
return false;
}
}
/**
* 保存文件文件到目录
* @param context
* @return 文件保存的目录
*/
public static String setMkdir(Context context)
{
String filePath;
if(checkSDCard())
{
filePath = Environment.getExternalStorageDirectory()+File.separator+"myfile";
}else{
filePath = context.getCacheDir().getAbsolutePath()+File.separator+"myfile";
}
File file = new File(filePath);
if(!file.exists())
{
boolean b = file.mkdirs();
Log.e("file", "文件不存在 创建文件 "+b);
}else{
Log.e("file", "文件存在");
}
return filePath;
}
}
下面就是界面上的逻辑处理:
[java]
package com.spring.sky.download;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Toast;
/**
* 文件下载界面
* @author spring sky
* Email:vipa1888@163.com
* QQ:840950105
* name:石明政
*
*/
public class IndexActivity extends Activity implements OnClickListener{
private static final int DOWNLOAD_PREPARE = 0;
private static final int DOWNLOAD_WORK = 1;
private static final int DOWNLOAD_OK = 2;
private static final int DOWNLOAD_ERROR =3;
private static final String TAG = "IndexActivity";
private Button bt ;
private ProgressBar pb;
private ImageView img;
/**
* 需要下载的文件
* 注意:在模拟器上面使用无法解析有域名的主机
*/
private String url = "http://61.155.165.32/shuixiyue/pic/item/f141247490d0e96fb251b963.jpg";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bt = (Button) this.findViewById(R.id.bt);
bt.setOnClickListener(this);
pb = (ProgressBar) this.findViewById(R.id.pb);
pb.setVisibility(ProgressBar.INVISIBLE);
img =(Im
补充:移动开发 , Android ,