我们在android开发过程中 经常有做到发图片或修改上传头像的功能 即要调用系统相册 如何调用系统相册并处理返回的数据呢?因为随着android手机系统的提高 不同系统的手机对调用相册并处理相册不同,所以我们就要做处理 让它可以兼容所有不同版本及系统的手机。

方法/步骤

  1. 进行相册有两种方式:一种是直接进入相册

    public static Intent INTENT_ACTION_PICK() {

    Intent intent = new Intent(Intent.ACTION_PICK,

    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

    intent.setType("image/*");

    return intent;

    }

    另一种是直接进入相册目录

    public static Intent INTENT_ACTION_CONTENT() {

    Intent intent = new Intent(Intent.ACTION_GET_CONTENT,

    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

    intent.setType("image/*");

    return intent;

    }

    startActivityForResult(intent,PICTURE);

  2. 写一个回调方法接收返回相册的路径

    public interface PhotoCallBack {   void onSuccess(String picturePath);// 拿取相片成功   void onFailure();// 拿取相片失败

    }

    这里写成回调方法的原因是 我们在开发过程中不仅仅是一个模块用到调用系统相册 写成一个回调方法 并在在一个类里处理返回的数据

  3. 根据SDK_INT来分类处理相册返回相片路径

    public static void getPhotoURLByAlbum(Context context, Intent data,      PhotoCallBack callback) {   if (data == null) {      callback.onFailure();      return;   }   final Uri selectedImage = data.getData();   if (selectedImage == null) {      callback.onFailure();      return;   }   String picturePath = "";   // 关于Android4.4的图片路径获取,如果回来的Uri的格式有两种   if (Build.VERSION.SDK_INT >= 19         && DocumentsContract.isDocumentUri(context, selectedImage)) {      String wholeID = DocumentsContract.getDocumentId(selectedImage);      String id = wholeID.split(":")[1];      String[] column = { MediaStore.Images.Media.DATA };      String sel = MediaStore.Images.Media._ID + "=?";      Cursor cursor = context.getContentResolver().query(            MediaStore.Images.Media.EXTERNAL_CONTENT_URI, column, sel,            new String[] { id }, null);      if (cursor.moveToNext()) {         int columnIndex = cursor.getColumnIndex(column[0]);         picturePath = cursor.getString(columnIndex);         callback.onSuccess(picturePath);// 获取图片路径      }      cursor.close();   } else {      String[] projection = { MediaStore.Images.Media.DATA };      Cursor cursor = context.getContentResolver().query(selectedImage,            projection, null, null, null);      if (cursor.moveToNext()) {         int column_index = cursor               .getColumnIndex(MediaStore.Images.Media.DATA);         picturePath = cursor.getString(column_index);         callback.onSuccess(picturePath);// 获取图片路径         Log.e("aa", "aa");      }      cursor.close();   }}

  4. 根据返回的相片路径URL来获得我们需要的Bitmap 我们就可以在ImageView直接显示了

    public static Bitmap getBitmapByURL(String PhotoURL) {   BitmapFactory.Options options = new BitmapFactory.Options()   options.inJustDecodeBounds = false;   Bitmap bitmap = BitmapFactory.decodeFile(PhotoURL, options);   if (bitmap != null) {      return bitmap;   }   return null;}   //   // 把图片保存到我们的系统图库中

    public static void saveMyImageToGallery(Context context, Bitmap bmp) {    File appDir = new File(Environment.getExternalStorageDirectory(), "Boohee");    if (!appDir.exists()) {        appDir.mkdir();    }    String fileName = System.currentTimeMillis() + ".jpg";    File file = new File(appDir, fileName);    try {        FileOutputStream fos = new FileOutputStream(file);        bmp.compress(CompressFormat.JPEG, 100, fos);        fos.flush();        fos.close();    } catch (FileNotFoundException e) {        e.printStackTrace();    } catch (IOException e) {        e.printStackTrace();       try {        MediaStore.Images.Media.insertImage(context.getContentResolver(),            file.getAbsolutePath(), fileName, null);    } catch (FileNotFoundException e) {        e.printStackTrace();    }}

photo的更多相关文章

  1. 在 Windows 10 中启用 Windows Photo Viewer

    本文版权归cxun所有,如有转载请注明出处与本文链接,谢谢!原文地址:http://www.cnblogs.com/cxun/p/4727323.html 不知大家在使用了Win10之后有没有这样感受 ...

  2. Autodesk的照片建模云服务—Autodesk ReCap 360 photo 的测试数据

    之前已经给大家介绍过了Autodesk的照片建模云服务—Autodesk ReCap 360 photo, 你也可以自己登录到http://recap360.autodesk.com/ 自己试一试. ...

  3. 如何申请Autodesk ReCap 360 photo的云币(Cloud Credit)

    在之前的博客中我介绍过Autodesk的照片建模云服务—Autodesk ReCap 360 photo,通过Autodesk ReCap 360 photo,你可以非常方便的通过照片生成三维模型.如 ...

  4. Autodesk的照片建模云服务—Autodesk ReCap 360 photo

    现实捕捉技术方兴未艾,简单的讲现实捕捉技术就是把现实中的现状信息数字化到计算机中以便做进一步的处理.对于不同的应用目的会有不同的捕捉设备,工程或传媒娱乐行业中经常用到的肯定就是三维模型了.那如何得到三 ...

  5. iOS开发之保存照片到系统相册(Photo Album)

    iOS开发之保存照片到系统相册(Photo Album) 保存照片到系统相册这个功能很多社交类的APP都有的,今天我们简单讲解一下,如何将图片保存到系统相册(Photo Album). 创建UIIma ...

  6. mongo里面根据对象字段的ID查询 db.Photo.find({'owner.$id':ObjectId('xxxx')}) , 并且使用forEach循环修改查询的数据

    var ones = db.Photo.find({'owner.$id':ObjectId("5344f0dab7c58e8e098b4567")}) db.Photo.find ...

  7. iOS - Photo Album 图片/相册管理

    前言 NS_CLASS_AVAILABLE_IOS(2_0) @interface UIImagePickerController : UINavigationController <NSCod ...

  8. WordPress Pretty Photo插件‘hashrel’参数跨站脚本漏洞

    漏洞名称: WordPress Pretty Photo插件‘hashrel’参数跨站脚本漏洞 CNNVD编号: CNNVD-201311-405 发布时间: 2013-11-28 更新时间: 201 ...

  9. 数据结构(线段树):BZOJ 3126: [Usaco2013 Open]Photo

    3126: [Usaco2013 Open]Photo Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 222  Solved: 116 Descrip ...

  10. [PWA] 18. Clean the photo cache

    We cannot let photo always keep caching new data without clean the old data. If message is not displ ...

随机推荐

  1. ie11强制兼容模式打开

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

  2. iOS关于友盟分享弹不出面板问题

    在程序代理类中声明 [NSThread sleepForTimeInterval:10];//设置启动页面时间 [self.window makeKeyAndVisible]; [[UMSocialM ...

  3. 用代码控制退出APP

    + (void)exitApplication { AppDelegate *app = [UIApplication sharedApplication].delegate; UIWindow *w ...

  4. JDBC基础学习(二)—PreparedStatement

    一.PreparedStatement介绍     在SQL中包含特殊字符或SQL的关键字(如: ' or 1 or ')时Statement将出现不可预料的结果(出现异常或查询的结果不正确),可用P ...

  5. Python爬虫入门 Urllib库的基本使用

    1.分分钟扒一个网页下来 怎样扒网页呢?其实就是根据URL来获取它的网页信息,虽然我们在浏览器中看到的是一幅幅优美的画面,但是其实是由浏览器解释才呈现出来的,实质它是一段HTML代码,加 JS.CSS ...

  6. 用 Python 编写剪刀、石头、布的小游戏(快速学习python语句)

    import random#定义手势类型allList = ['石头','剪刀','布']#定义获胜的情况winList = [['石头','剪刀'],['剪刀','布'],['步','石头']]pr ...

  7. 关于sql语句between and的边界问题

    BETWEEN 操作符 操作符 BETWEEN ... AND 会选取介于两个值之间的数据范围.这些值可以是数值.文本或者日期. SQL BETWEEN 语法 SELECT column_name(s ...

  8. ios 添加工程依赖只能生成Generic Xcode Archive 文件原因

    问题说明:工程引用了外部类库, 默认生成的archive是 Generic Xcode Archive 格式的 无法发布和生成ipa文件. 解决处理: 1.将Build Settings->De ...

  9. 解决EditText不能撑满全屏的问题及EditText你应该知道的属性

    一般我们要实现去下图一的效果很简单: 两个EditText就搞定 效果图一:   但是我们想让第二个EditText撑满剩余空间怎么做?如效果图二 效果图二: 解决: 使用了ScrollView嵌套L ...

  10. Jquery对select下拉框的操作

    一.jQuery获取Select选择的Text和Value:语法解释: $("#select_id").change(function(){//code...});   //为Se ...