发布于 2016-02-28 03:37:38 | 280 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

Android移动端操作系统

Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用“安卓”或“安致”。


这篇文章主要介绍了Android实现从网络获取图片显示并保存到SD卡的方法,涉及Android操作多媒体文件及系统硬件设备的相关技巧,需要的朋友可以参考下

本文实例讲述了Android实现从网络获取图片显示并保存到SD卡的方法。分享给大家供大家参考,具体如下:

问题:

如何不断获取图片并显示出来,达到视频的效果?

代码:


public class GetPictureFromInternetActivity extends Activity 
{ 
 private ImageView imageView; 
 public void onCreate(Bundle savedInstanceState) 
 { 
 super.onCreate(savedInstanceState); 
 setContentView(R.layout.main); 
  String url = "http://img1.gcimg.net/att/day_120330/1203301402671605a8a7994804.png"; 
// String url = "http://www.gezila.com/uploads/allimg/110110/1_110110084544_1.jpg"; 
 imageView = (ImageView) this.findViewById(R.id.imageView); 
 Bitmap bitmap = getHttpBitmap(url);//从网络获取图片 
 imageView.setImageBitmap(bitmap); 
 savePicture(bitmap);//保存图片到SD卡 
 } 
 public Bitmap getHttpBitmap(String url) 
 { 
 Bitmap bitmap = null; 
 try 
 { 
  URL pictureUrl = new URL(url); 
  InputStream in = pictureUrl.openStream(); 
  bitmap = BitmapFactory.decodeStream(in); 
  in.close(); 
 } catch (MalformedURLException e) 
 { 
  e.printStackTrace(); 
 } catch (IOException e) 
 { 
  e.printStackTrace(); 
 } 
 return bitmap; 
 } 
 public void savePicture(Bitmap bitmap) 
 { 
 String pictureName = "/mnt/sdcard/" + "car"+".jpg"; 
 File file = new File(pictureName); 
 FileOutputStream out; 
 try 
 { 
  out = new FileOutputStream(file); 
  bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out); 
  out.flush(); 
  out.close(); 
 } catch (FileNotFoundException e) 
 { 
  e.printStackTrace(); 
 } catch (IOException e) 
 { 
  e.printStackTrace(); 
 } 
 } 
 public boolean onCreateOptionsMenu(Menu menu) 
 { 
 super.onCreateOptionsMenu(menu); 
 MenuItem item = menu.add(Menu.NONE, Menu.NONE, Menu.NONE, "Exit"); 
 item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() 
 { 
  public boolean onMenuItemClick(MenuItem item) 
  { 
  System.exit(0); 
  return true; 
  } 
 }); 
 return true; 
 } 
}

注意:1、权限问题

涉及网络时的权限:

<uses-permission android:name="android.permission.INTERNET"/>

涉及SD卡读写权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

问题分解:

问题1、如何从网络获取图片并显示:

问题2、如何不断显示图片:

扩展:如何保存获取到的图片:

问题1解决方案:

看似有三种选择方案,其实质就一种模式,换汤不换药。先通过统一资源定位器URl(uniform resource location)获取一个读取图片流,然后将其解压成Bitmap,最后显示出来。具体实现代码如下:

选择1:直接类URL打开一个流,最简单实用。


public Bitmap getHttpBitmap(String url) 
{ 
 Bitmap bitmap = null; 
 try 
 { 
  URL pictureUrl = new URL(url); 
  InputStream in = pictureUrl.openStream(); 
  bitmap = BitmapFactory.decodeStream(in); 
  in.close(); 
 } catch (MalformedURLException e) 
 { 
  e.printStackTrace(); 
 } catch (IOException e) 
 { 
  e.printStackTrace(); 
 } 
 return bitmap; 
}

选择2:用到类URLConnection打开连接


public Bitmap getHttpBitmap(String url) 
{ 
 Bitmap bitmap = null; 
 try 
 { 
  URL pictureUrl = new URL(url); 
  URLConnection con = pictureUrl.openConnection(); 
  InputStream in = con.getInputStream(); 
  bitmap = BitmapFactory.decodeStream(in); 
  in.close(); 
 } catch (MalformedURLException e) 
 { 
  e.printStackTrace(); 
 } catch (IOException e) 
 { 
  e.printStackTrace(); 
 } 
 return bitmap; 
}

选择3:用到类HttpURLConnection打开连接


public Bitmap getHttpBitmap(String url) 
{ 
 Bitmap bitmap = null; 
 try 
 { 
  URL pictureUrl = new URL(url); 
  HttpURLConnection con = (HttpURLConnection) pictureUrl.openConnection(); 
  InputStream in = con.getInputStream(); 
  bitmap = BitmapFactory.decodeStream(in); 
  in.close(); 
 } catch (MalformedURLException e) 
 { 
  e.printStackTrace(); 
 } catch (IOException e) 
 { 
  e.printStackTrace(); 
 } 
 return bitmap; 
}

问题2解决方案:

很容易想到开启一个定时器,每隔多久执行一次。

还有一种方案就是开一个线程,在while死循环里面用一个sleep睡一会儿。

保存获取到的图片解决方法:

保存图片,自然就涉及到SD卡上文件读写操作,这里是将Bitmap直接写入文件。联想到肯定要用到流,想到这就好办事了,不过还需要了解到BitmapFactory类的强大之处,这里展示了用系统时间为保存文件名称的实现过程,有一个好处就是可以任意保存,无需考虑覆盖和越界问题。


public void savePicture(Bitmap bitmap) 
{ 
 if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) 
 { 
 try 
 { 
 File sdcardDir = Environment 
 .getExternalStorageDirectory(); 
 Time t = new Time(); 
 t.setToNow();
 String filename = sdcardDir.getCanonicalPath() 
 + "/DCIM/camera" 
 + String.format( 
 "/ReeCam%04d%02d%02d%02d%02d%02d.jpg", 
 t.year, t.month + 1, t.monthDay, 
 t.hour, t.minute, t.second); 
 File file = new File(filename); 
 FileOutputStream out = new FileOutputStream(file); 
 bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out); 
 out.flush(); 
 out.close(); 
 } catch (FileNotFoundException e) 
 { 
 e.printStackTrace(); 
 } catch (IOException e) 
 { 
 e.printStackTrace(); 
 }
 } 
}

注释:这里用到的bitmap就是上面生成的bitmap。

看到这个问题就感觉像是高中时的综合题目一样,将其分解成简单的问题,将每个小问题解决,那么复杂问题自然就可以解决了。记得前几天看了篇帖子,主题是“当问题被分解成更小的问题后,所有的问题都变得如此简单,而且所有的问题都能这样去分解。”何为牛人,就是遇到复杂问题时,能保持清晰的思路,分析问题的流程,然后将其分解成足够小的问题,一个个解决,最后再组合。就如看到一辆小车,零件之多,有点小复杂吧,然而我们如下去分解:四个轮子和车壳,然后轮子再分而钢圈和轮胎皮, 轮胎皮再分解为内胎和外胎。然后你要做的事就是找到生产轮胎和钢圈的厂家购买这两样组件,然后再利用第三方或者其它工具去组装成车轮。这里轮胎和钢圈相当于Java里面类,第三方或其他组装工具,就如你的代码,将它们和发动机组装再一起就实现了车子跑到的功能。学会分解思维,最常用的就是二分法,当然还得具体问题具体分析。

希望本文所述对大家Android程序设计有所帮助。



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务