Android学习之图片(一)——从相机和相册取图
来源:互联网 发布:臭苋菜 知乎 编辑:程序博客网 时间:2024/05/21 11:29
这是基本技能了,只是温习一下
转载请注明出处
[我的博客]http://www.lostbug.com
- 相机取图
一般取原图(Save the Full-size Photo),缩略图用处不多
首先先建个文件,以便写入照片,并保存路径,以方便后期使用
String mCurrentPhotoPath;private File createImageFile() throws IOException { // Create an image file name String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); String imageFileName = "JPEG_" + timeStamp + "_"; File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES); File image = File.createTempFile( imageFileName, /* prefix */ ".jpg", /* suffix */ storageDir /* directory */ ); // Save a file: path for use with ACTION_VIEW intents mCurrentPhotoPath = "file:" + image.getAbsolutePath(); return image;}
然后创建和调用Intent
static final int REQUEST_TAKE_PHOTO = 1;private void dispatchTakePictureIntent() { Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); // Ensure that there's a camera activity to handle the intent if (takePictureIntent.resolveActivity(getPackageManager()) != null) { // Create the File where the photo should go File photoFile = null; try { photoFile = createImageFile(); } catch (IOException ex) { // Error occurred while creating the File ... } // Continue only if the File was successfully created if (photoFile != null) { Uri photoURI = Uri.fromFile(photoFile); takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI); startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO); } }}
然后在onActivityResult里引用文件路径获取图片就行了
- 相册取图
创建 并调用Intent
private static final int RESULT_ALBUM_IMAGE=2;Intent i = new Intent( Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(i, RESULT_ALBUM_IMAGE);
在onActivityResult里获取图片:
if (requestCode == RESULT_ALBUM_IMAGE && resultCode == RESULT_OK && data != null) { Uri selectedImage = data.getData(); String[] filePathColumn = { MediaStore.Images.Media.DATA }; Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null); cursor.moveToFirst(); int columnIndex = cursor.getColumnIndex(filePathColumn[0]); final String picturePath = cursor.getString(columnIndex); Log.d("PICTUREPATH", picturePath); cursor.close(); Bitmap bitmap=BitmapFactory.decodeFile(picturePath); mImageView.setImageBitmap(bitmap); }
就是这样啦
0 0
- Android学习之图片(一)——从相机和相册取图
- Android从相册和相机获取图片介绍
- Android 从相册和相机选取图片并剪切
- Android选择图片(从相册或相机)并裁剪
- Android 从相机或相册获取图片
- android开发之上传图片和调用相机以及相册
- 相机的调用以及从相册中选取图片
- iOS图片处理(一)————调用系统相机和相册获取图片,给相机添加自定义覆盖物
- android实现从相册和相机中取照片并剪辑
- android 调用相机和获取相册图片
- Android中使用OKHttp上传图片,从相机和相册中获取图片并剪切
- android上传图片(相机,相册)
- 基础Android之调用相机和相册
- Android从相机或相册获取图片裁剪
- Android从相机、相册选择图片并裁剪
- Android 从相机或相册获取图片并裁剪
- Android 调用相机或从相册获取图片
- Android 从相机或相册获取图片并剪切
- oracle内存管理手动到自动
- Git Stash 命令
- 提交App至Appstore问题
- 用VLC做流媒体服务器
- unity 5.x android发布注意事项
- Android学习之图片(一)——从相机和相册取图
- android 发送有序广播sendOrderBraodCast、截断广播和广播之间的数据传递
- redis集群一致性哈希--哈希槽
- 项目五大过程组
- Cpp环境【NOIP201307】货车运输
- NSDate和NSCalendar的简单应用
- R函数-diag()
- php开发那点事儿(二)
- Espresso 2.0+ 报java.lang.NoClassDefFoundError