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

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

服务器之家 - 编程语言 - Android - Android实现从相册截图的功能

Android实现从相册截图的功能

2021-05-11 16:58Kriss Android

这篇文章主要介绍了Android实现从相册截图的功能,简单介绍了Android实现从相册截图功能的步骤,供大家参考,感兴趣的小伙伴们可以参考一下

在这篇文章中,我将向大家展示如何从相册截图。
先看看效果图:

Android实现从相册截图的功能

Android实现从相册截图的功能

上一篇文章中,我就拍照截图这一需求进行了详细的分析,试图让大家了解android本身的限制,以及我们应当采取的实现方案。大家可以回顾一下:android实现拍照截图功能

根据我们的分析与总结,图片的来源有拍照和相册,而可采取的操作有

  • 使用bitmap并返回数据
  • 使用uri不返回数据

前面我们了解到,使用bitmap有可能会导致图片过大,而不能返回实际大小的图片,我将采用大图uri,小图bitmap的数据存储方式。

我们将要使用到uri来保存拍照后的图片:

?
1
2
3
4
5
6
7
8
9
10
private
 
static
final
string image_file_location = "file:///sdcard/temp.jpg";//temp
 file
 
uri
 imageuri = uri.parse(image_file_location);//the
 uri to store the big bitmap

不难知道,我们从相册选取图片的action为intent.action_get_content。

根据我们上一篇博客的分析,我准备好了两个实例的intent。

一、从相册截大图:

?
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
intent
 intent = new
 
intent(intent.action_get_content, null);
 
intent.settype("image/*");
 
intent.putextra("crop",
"true");
 
intent.putextra("aspectx",
2);
 
intent.putextra("aspecty",
1);
 
intent.putextra("outputx",
600);
 
intent.putextra("outputy",
300);
 
intent.putextra("scale",
true);
 
intent.putextra("return-data",
false);
 
intent.putextra(mediastore.extra_output,
 imageuri);
 
intent.putextra("outputformat",
 bitmap.compressformat.jpeg.tostring());
 
intent.putextra("nofacedetection",
true);
//
 no face detection
 
startactivityforresult(intent,
 choose_big_picture);

二、从相册截小图

?
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
intent
 intent = new
 
intent(intent.action_get_content, null);
 
intent.settype("image/*");
 
intent.putextra("crop",
"true");
 
intent.putextra("aspectx",
2);
 
intent.putextra("aspecty",
1);
 
intent.putextra("outputx",
200);
 
intent.putextra("outputy",
100);
 
intent.putextra("scale",
true);
 
intent.putextra("return-data",
true);
 
intent.putextra("outputformat",
 bitmap.compressformat.jpeg.tostring());
 
intent.putextra("nofacedetection",
true);
//
 no face detection
 
startactivityforresult(intent,
 choose_small_picture);

三、对应的onactivityresult可以这样处理返回的数据

?
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
switch
 
(requestcode) {
 
case
 
choose_big_picture:
 
  log.d(tag,
"choose_big_picture:
 data = "
+ data);//it
 seems to be null
 
  if(imageuri
 != null){
 
    bitmap
 bitmap = decodeuriasbitmap(imageuri);//decode
 bitmap
 
    imageview.setimagebitmap(bitmap);
 
  }
 
  break;
 
case
 
choose_small_picture:
 
  if(data
 != null){
 
    bitmap
 bitmap = data.getparcelableextra("data");
 
    imageview.setimagebitmap(bitmap);
 
  }else{
 
    log.e(tag,
"choose_small_picture:
 data = "
+ data);
 
  }
 
  break;
 
default:
 
  break;
 
}
 
 
private
 
bitmap decodeuriasbitmap(uri uri){
 
  bitmap
 bitmap = null;
 
  try
 
{
 
    bitmap
 = bitmapfactory.decodestream(getcontentresolver().openinputstream(uri));
 
  }
catch
 
(filenotfoundexception e) {
 
    e.printstacktrace();
 
    return
 
null;
 
  }
 
  return
 
bitmap;
 
}

以上就是android实现拍照截图功能的方法,希望对大家的学习有所帮助。

延伸 · 阅读

精彩推荐