Android读取本地图片和网络图片:
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.main);
   ImageView image = (ImageView) findViewById(R.myImage.imageview);
   //Bitmap bitmap = getLoacalBitmap(“/images/1.jpg”); //从本地取图片
   Bitmap bitmap = getHttpBitmap(“http://t1.baidu.com/it/u=1966865594,1522122112&fm=52&gp=0.jpg”); //从网上取图片

   image .setImageBitmap(bitmap); //设置Bitmap
}
/**
* 加载本地图片
* @param url
* @return
*/
public static Bitmap getLoacalBitmap(String url) {
try {
    FileInputStream fis = new FileInputStream(url);
    return BitmapFactory.decodeStream(fis);
} catch (FileNotFoundException e) {
    e.printStackTrace();
    return null;
}
}

/**
* 加载网络图片
* @param url
* @return
*/
public static Bitmap getHttpBitmap(String url) {
  URL myFileUrl = null;
  Bitmap bitmap = null;
try {
   myFileUrl = new URL(url);
} catch (MalformedURLException e) {
   e.printStackTrace();
}
try {
   HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
   conn.setConnectTimeout(0);
   conn.setDoInput(true);
   conn.connect();
  InputStream is = conn.getInputStream();
  bitmap = BitmapFactory.decodeStream(is);
  is.close();
} catch (IOException e) {
  e.printStackTrace();
}
return bitmap;
}


注意:在AndroidManifest.xml文件的节点上面添加

        android:name="android.permission.INTERNET"
        />,这是由于Android有很多的权限限制,否则图片是不能在你的模拟器上显示的。

更多相关文章

  1. android 用tcpdump抓取网络包
  2. android:获取网络时间、文件路径,修改包名,发布release版本
  3. Android 10 获取相册图片失败
  4. Android图片旋转实例
  5. Android获取图片Uri/path
  6. 【Android随记】-- Android动态加载布局
  7. android在service中使用AsyncHttpClient加载网络资源
  8. android 网络语音电话合集 此文为备份
  9. android 网络异步加载数据进度条

随机推荐

  1. 哪些手机可升级安卓5.0?Android(安卓)5.0
  2. Android(安卓)4.0的图形硬件加速及绘制技
  3. Android帧缓冲区(Frame Buffer)硬件抽象层(H
  4. 【Android信息安防】三星KNOX容器(1)
  5. Android(安卓)自定义View实现动画效果切
  6. Android(安卓)Handler机制不一样角度去理
  7. 移动互联网和Android给你带来的机会[轉]
  8. Android学习记录(4)—在java中学习多线程下
  9. android ndk生成第三方库的so方法(ndk-bu
  10. android获取手机号码与IMEI码