1.???????????
?????????
-
<?xml version="1.0" encoding="utf-8"?>
-
<LinearLayout
-
xmlns:android="http://schemas.android.com/apk/res/android"
-
android:orientation="vertical"
-
android:layout_width="fill_parent"
-
android:layout_height="fill_parent">
-
-
<ImageView android:src="@drawable/icon"
-
android:layout_width="wrap_content"
-
android:id="@+id/imgPic"
-
android:layout_gravity="center|center_vertical"
-
android:layout_height="fill_parent">
-
</ImageView>
-
-
</LinearLayout>
???????
java???
-
public class DownloadImage extends Activity {
-
private ImageView imgPic;
-
-
@Override
-
protected void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.download_image);
-
imgPic = (ImageView) findViewById(R.id.imgPic);
-
String url = "http://ww1.sinaimg.cn/bmiddle/6834c769jw1djjf4p3p9rj.jpg";
-
loadRmoteImage(url);
-
}
-
-
/**
-
* @param imgUrl
-
* ??????????URL
-
*
-
* ?????????
-
*/
-
private void loadRmoteImage(String imgUrl) {
-
URL fileURL = null;
-
Bitmap bitmap = null;
-
try {
-
fileURL = new URL(imgUrl);
-
} catch (MalformedURLException err) {
-
err.printStackTrace();
-
}
-
try {
-
HttpURLConnection conn = (HttpURLConnection) fileURL
-
.openConnection();
-
conn.setDoInput(true);
-
conn.connect();
-
InputStream is = conn.getInputStream();
-
int length = (int) conn.getContentLength();
-
if (length != -1) {
-
byte[] imgData = new byte[length];
-
byte[] buffer = new byte[512];
-
int readLen = 0;
-
int destPos = 0;
-
while ((readLen = is.read(buffer)) > 0) {
-
System.arraycopy(buffer, 0, imgData, destPos, readLen);
-
destPos += readLen;
-
}
-
bitmap = BitmapFactory.decodeByteArray(imgData, 0,
-
imgData.length);
-
}
-
} catch (IOException e) {
-
e.printStackTrace();
-
}
-
imgPic.setImageBitmap(bitmap);
-
}
???????
2.??????????????
???????????????????????????????????????????????????????????????????????????¡Â???
-
public class DownloadImage extends Activity {
-
private ImageView imgPic;
-
private ProgressBar progressBar;
-
private int totalSize=0;
-
private int size=0;
-
private Handler mHandler;
-
String url = "http://ww1.sinaimg.cn/bmiddle/6834c769jw1djjf4p3p9rj.jpg";
-
private Bitmap bitmap=null;
-
@Override
-
protected void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.download_image);
-
imgPic = (ImageView) findViewById(R.id.imgPic);
-
-
progressBar = (ProgressBar) findViewById(R.id.progressBar);
-
progressBar.setProgress(getProgressInt(progressBar.getMax()));
-
mHandler = new Handler() {
-
public void handleMessage(Message msg) {
-
progressBar.setProgress(getProgressInt(progressBar.getMax()));
-
if(bitmap!=null){
-
imgPic.setImageBitmap(bitmap);
-
}
-
}
-
};
-
new Thread(){
-
public void run(){
-
loadRmoteImage(url);
-
}
-
}.start();
-
}
-
-
/**
-
* @param imgUrl
-
* ??????????URL
-
*
-
* ?????????
-
*/
-
private void loadRmoteImage(String imgUrl) {
-
URL fileURL = null;
-
try {
-
fileURL = new URL(imgUrl);
-
} catch (MalformedURLException err) {
-
err.printStackTrace();
-
}
-
try {
-
HttpURLConnection conn = (HttpURLConnection) fileURL
-
.openConnection();
-
conn.setDoInput(true);
-
conn.connect();
-
InputStream is = conn.getInputStream();
-
int length = (int) conn.getContentLength();
-
totalSize=length;
-
if (length != -1) {
-
byte[] imgData = new byte[length];
-
byte[] buffer = new byte[512];
-
int readLen = 0;
-
int destPos = 0;
-
while ((readLen = is.read(buffer)) > 0) {
-
System.arraycopy(buffer, 0, imgData, destPos, readLen);
-
destPos += readLen;
-
size=destPos;
-
mHandler.sendEmptyMessage(1);
-
Thread.sleep(100);
-
}
-
bitmap = BitmapFactory.decodeByteArray(imgData, 0,
-
imgData.length);
-
mHandler.sendEmptyMessage(1);
-
}
-
} catch (IOException e) {
-
e.printStackTrace();
-
} catch (InterruptedException e) {
-
e.printStackTrace();
-
}
-
-
}
-
private int getProgressInt(int max) {
-
int result = (totalSize > 0) ? (int) (size * max * 1.0 / totalSize) : 0;
-
return result;
-
}
-
}
???????
????????????????????Bitmap???????????????????????byte[]????????bitmap????????????InputStream????bitmap??
? ????????byte[]????????bitmap
onCreate{
-
byte[] data = getImage(filePath);
-
if(data!=null){
-
bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);// bitmap
-
imageView.setImageBitmap(bitmap);// display image
-
}else{
-
Toast.makeText(AndroidTest2_3_3.this, "Image error!", 1).show();
-
}
???????
}
-
public byte[] getImage(String path) throws Exception{
-
URL url = new URL(path);
-
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
-
conn.setConnectTimeout(5 * 1000);
-
conn.setRequestMethod("GET");
-
InputStream inStream = conn.getInputStream();
-
if(conn.getResponseCode() == HttpURLConnection.HTTP_OK){
-
return readStream(inStream);
-
}
-
return null;
-
}
-
public static byte[] readStream(InputStream inStream) throws Exception{
-
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
-
byte[] buffer = new byte[1024];
-
int len = 0;
-
while( (len=inStream.read(buffer)) != -1){
-
outStream.write(buffer, 0, len);
-
}
-
outStream.close();
-
inStream.close();
-
return outStream.toByteArray();
-
}
?????????InputStream
onCreate{
-
bitmap = BitmapFactory.decodeStream(getImageStream(filePath));
-
if (bitmap != null) {
-
imageView.setImageBitmap(bitmap);// display image
-
}
-
//********************************************************************/
-
Log.d(TAG, "set image ...");
-
} catch (Exception e) {
-
Toast.makeText(AndroidTest2_3_3.this,"Newwork error!", 1).show();
-
e.printStackTrace();
-
}
-
}
-
public InputStream getImageStream(String path) throws Exception{
-
URL url = new URL(path);
-
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
-
conn.setConnectTimeout(5 * 1000);
-
conn.setRequestMethod("GET");
-
if(conn.getResponseCode() == HttpURLConnection.HTTP_OK){
-
return conn.getInputStream();
-
}
-
return null;
-
}
???????
-
public void saveFile(Bitmap bm, String fileName) throws IOException {
-
File dirFile = new File(ALBUM_PATH);
-
if(!dirFile.exists()){
-
dirFile.mkdir();
-
}
-
File myCaptureFile = new File(ALBUM_PATH + fileName);
-
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
-
bm.compress(Bitmap.CompressFormat.JPEG, 80, bos);
-
bos.flush();
-
bos.close();
-
}
|