android读取远程图片案例

时间:2024-08-13 20:37:38

关键代码:
Bitmap bitmap=BitmapFactory.decodeByteArray(data, 0, data.length);
imageview.setImageBitmap(bitmap);

注意访问网络权限:<uses-permission android:name="android.permission.INTERNET"/>

完整测试代码如下:

 package caicai.cn.netimag;

  import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL; public class imageserver {
public static byte[] getimage(String path) throws Exception{ //连接远程网址
URL url=new URL(path);
HttpURLConnection conn=(HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
if(conn.getResponseCode()==200){
InputStream instream=conn.getInputStream();
return read(instream);
}
return null;
} private static byte[] read(InputStream instream) throws Exception{ // 读取数据流,返回字节数据流
ByteArrayOutputStream outstream=new ByteArrayOutputStream();
byte[] buffer=new byte[1024];
while( (instream.read(buffer))!=-1){
outstream.write(buffer);
}
instream.close();
return outstream.toByteArray();
}
}

imageserver.java

 package caicai.cn.netimag;

  import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast; public class NetimageActivity extends Activity { public ImageView imageview;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageview=(ImageView) findViewById(R.id.imageview);
}
public void submit(View v){
String path="http://192.168.0.117/testxml/bottom.png";
try{
byte[] data=imageserver.getimage(path);
Bitmap bitmap=BitmapFactory.decodeByteArray(data, 0, data.length); //生成图片工厂
imageview.setImageBitmap(bitmap); //显示图片
}catch(Exception e){ Toast.makeText(getApplicationContext(), "出错了", 1).show();
}
}
}

NetimageActivity.java

 <?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:text="获取网络图片"
android:onClick="submit"/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/imageview"
android:src="@drawable/ic_launcher"
/> </LinearLayout>

main.xml

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="caicai.cn.netimag"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET"/> <application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".NetimageActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application> </manifest>

AndroidManifest.xml

更新最新加载方法

	/**
* 根据一个网络连接(String)获取bitmap图像
*
* @param imageUri
* @return
* @throws MalformedURLException
*/
public static Bitmap getbitmap(String imageUri) {
// 显示网络上的图片
Bitmap bitmap = null;
try {
URL myFileUrl = new URL(imageUri);
HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
bitmap = BitmapFactory.decodeStream(is);
is.close();
} catch (IOException e) {
e.printStackTrace();
return null;
}
return bitmap;
}