服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Android实现从网络获取图片显示并保存到SD卡的方法

Android实现从网络获取图片显示并保存到SD卡的方法

2021-04-19 16:17Sunnyfans Android

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

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

问题:

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

代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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打开一个流,最简单实用。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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打开连接

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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打开连接

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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类的强大之处,这里展示了用系统时间为保存文件名称的实现过程,有一个好处就是可以任意保存,无需考虑覆盖和越界问题。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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程序设计有所帮助。

延伸 · 阅读

精彩推荐