android读取远程图片案例
关键代码:
Bitmap bitmap=BitmapFactory.decodeByteArray(data, 0, data.length);
imageview.setImageBitmap(bitmap);
注意访问网络权限:<uses-permission android:name="android.permission.INTERNET"/>
完整测试代码如下:
1 package caicai.cn.netimag; 2 3 import java.io.ByteArrayOutputStream; 4 import java.io.InputStream; 5 import java.net.HttpURLConnection; 6 import java.net.URL; 7 8 public class imageserver { 9 public static byte[] getimage(String path) throws Exception{ //连接远程网址 10 URL url=new URL(path); 11 HttpURLConnection conn=(HttpURLConnection) url.openConnection(); 12 conn.setConnectTimeout(5000); 13 conn.setRequestMethod("GET"); 14 if(conn.getResponseCode()==200){ 15 InputStream instream=conn.getInputStream(); 16 return read(instream); 17 } 18 return null; 19 } 20 21 private static byte[] read(InputStream instream) throws Exception{ // 读取数据流,返回字节数据流 22 ByteArrayOutputStream outstream=new ByteArrayOutputStream(); 23 byte[] buffer=new byte[1024]; 24 while( (instream.read(buffer))!=-1){ 25 outstream.write(buffer); 26 } 27 instream.close(); 28 return outstream.toByteArray(); 29 } 30 }
1 package caicai.cn.netimag; 2 3 import android.app.Activity; 4 import android.graphics.Bitmap; 5 import android.graphics.BitmapFactory; 6 import android.os.Bundle; 7 import android.view.View; 8 import android.widget.ImageView; 9 import android.widget.Toast; 10 11 public class NetimageActivity extends Activity { 12 13 public ImageView imageview; 14 public void onCreate(Bundle savedInstanceState) { 15 super.onCreate(savedInstanceState); 16 setContentView(R.layout.main); 17 imageview=(ImageView) findViewById(R.id.imageview); 18 } 19 public void submit(View v){ 20 String path="http://192.168.0.117/testxml/bottom.png"; 21 try{ 22 byte[] data=imageserver.getimage(path); 23 Bitmap bitmap=BitmapFactory.decodeByteArray(data, 0, data.length); //生成图片工厂 24 imageview.setImageBitmap(bitmap); //显示图片 25 }catch(Exception e){ 26 27 Toast.makeText(getApplicationContext(), "出错了", 1).show(); 28 } 29 } 30 }
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="fill_parent" 4 android:layout_height="fill_parent" 5 android:orientation="vertical" > 6 7 <Button 8 android:layout_width="fill_parent" 9 android:layout_height="wrap_content" 10 android:text="获取网络图片" 11 android:onClick="submit"/> 12 <ImageView 13 android:layout_width="fill_parent" 14 android:layout_height="wrap_content" 15 android:id="@+id/imageview" 16 android:src="@drawable/ic_launcher" 17 /> 18 19 </LinearLayout>
1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="caicai.cn.netimag" 4 android:versionCode="1" 5 android:versionName="1.0" > 6 7 <uses-sdk android:minSdkVersion="8" /> 8 <uses-permission android:name="android.permission.INTERNET"/> 9 10 <application 11 android:icon="@drawable/ic_launcher" 12 android:label="@string/app_name" > 13 <activity 14 android:label="@string/app_name" 15 android:name=".NetimageActivity" > 16 <intent-filter > 17 <action android:name="android.intent.action.MAIN" /> 18 19 <category android:name="android.intent.category.LAUNCHER" /> 20 </intent-filter> 21 </activity> 22 </application> 23 24 </manifest>
更新最新加载方法
/** * 根据一个网络连接(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; }
posted on 2013-06-23 17:11 clarenceV1 阅读(1602) 评论(0) 编辑 收藏 举报