Android实现从相册截图的功能
作者:Kriss ❀
这篇文章主要介绍了Android实现从相册截图的功能,简单介绍了Android实现从相册截图功能的步骤,供大家参考,感兴趣的小伙伴们可以参考一下
在这篇文章中,我将向大家展示如何从相册截图。
先看看效果图:


上一篇文章中,我就拍照截图这一需求进行了详细的分析,试图让大家了解Android本身的限制,以及我们应当采取的实现方案。大家可以回顾一下:Android实现拍照截图功能
根据我们的分析与总结,图片的来源有拍照和相册,而可采取的操作有
- 使用Bitmap并返回数据
- 使用Uri不返回数据
前面我们了解到,使用Bitmap有可能会导致图片过大,而不能返回实际大小的图片,我将采用大图Uri,小图Bitmap的数据存储方式。
我们将要使用到URI来保存拍照后的图片:
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。
一、从相册截大图:
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);
二、从相册截小图
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可以这样处理返回的数据
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实现拍照截图功能的方法,希望对大家的学习有所帮助。
您可能感兴趣的文章:
- Native.js获取监听开关等操作Android蓝牙设备实例代码
- native.js获取手机硬件基本信息实例代码android版
- Dcloud的native.js直接拨打电话Android实例代码
- DCloud的native.js调用系统分享实例Android版代码
- Android中通过view方式获取当前Activity的屏幕截图实现方法
- Android中如何获取视频文件的截图、缩略图
- Android模拟器中窗口截图存成文件实现思路及代码
- 详解有关Android截图与录屏功能的学习
- Android实现截图和分享功能的代码
- Android获取常用辅助方法(获取屏幕高度、宽度、密度、通知栏高度、截图)
- Android实现拍照截图功能
- android截图事件监听的原理与实现
- Android屏幕及view的截图实例详解
- Android截屏截图的几种方法总结
- Android实现截图分享qq 微信功能
- Android 中WebView 截图的实现方式
- Android App内监听截图加二维码功能代码
- Android 5.0及以上编程实现屏幕截图功能的方法
- Android仿银行客户签名并且保存签名的截图文件并命名为本地时间
- Android 截图功能源码的分析
- Android使用WebView实现截图分享功能
- Native.js屏幕截图实例代码
