android 获取图片
Android获取手机或者内存卡里面的图片有两种方式
1.这是通过一种action
Intent intent=new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, MAIN_PIC);
2.这是通过另一个中action
Intent intent=new Intent();
intent.setAction(Intent.ACTION_PICK);
//EXTERNAL_CONTENT_URI 是外部的存储uri
// INTERNAL_CONTENT_URI是内部的存储uri
intent.setData(MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, MAIN_PIC);
然后在onActivityResult里面进行操作
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
Uri uri = data.getData();
Bitmap bitmap;
try {
bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));
imageview.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/* 将Bitmap设定到ImageView */
String[] proj = {MediaStore.Images.Media.DATA};
//好像是android多媒体数据库的封装接口,具体的看Android文档
Cursor cursor = managedQuery(uri, proj, null, null, null);
//按我个人理解 这个是获得用户选择的图片的索引值
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
//将光标移至开头 ,这个很重要,不小心很容易引起越界
cursor.moveToFirst();
//最后根据索引值获取图片路径
String path = cursor.getString(column_index);
Toast.makeText(this, path, 1).show();
}
得到uri之后,一般需要查询,然后得到结果集,里面就是图片的路径。
case REQ_CODE_PICTURE:
Uri uri = data.getData();
Cursor cursor = getContentResolver().query(uri, null, null,
null, null);
cursor.moveToFirst();
try {
srcpath = cursor.getString(1);
Log.i("OnActivtyResult",
"File path :[" + cursor.getColumnCount() + srcpath
+ "]");
//MediaStore.Images.Media.DATA
InputStream is = new FileInputStream(cursor.getString(1));
Bitmap bmp = ImageLoader.createBitmap(is, 1);
ivIcon.setImageBitmap(bmp);
} catch (Exception e) {
e.printStackTrace();
}
break;
}
protected String getAbsoluteImagePath(Uri uri) {
// can post image
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, proj, // Which columns to return
null, // WHERE clause; which rows to return (all rows)
null, // WHERE clause selection arguments (none)
null); // Order-by clause (ascending by name)
if (cursor != null) {
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} else {
// 如果游标为空说明获取的已经是绝对路径了
return uri.getPath();
}
}
android 获取图片的更多相关文章
- Android 获取图片exif信息
使用android api读取图片的exif信息 布局代码: <LinearLayout xmlns:android="http://schemas.android.com/apk/r ...
- android 获取 图片或视频略缩图
/** * 根据指定的图像路径和大小来获取缩略图 此方法有两点好处: 1. * 使用较小的内存空间,第一次获取的bitmap实际上为null,只是为了读取宽度和高度, * 第二次读取的bitmap是根 ...
- Android获取图片实际大小兼容平板电脑
项目中有个图片在平板电脑中显示特别小的原因.一直苦于没找到原因,也没有平板电脑測试,今天找了个改动分辨率的,编写相关方法最终处理了,记录下比較: 好让以后不造轮子. 主要是获取文章相关图片显示问题.直 ...
- Android 获取图片真实宽高
Resources res = mContext.getResources(); BitmapFactory.Options opts = new BitmapFactory.Options(); o ...
- Android 获取图片转bitmap
1. Resources resources = mContext.getResources(); Drawable drawable = resources.getDrawable(R.drawab ...
- Java乔晓松-android中获取图片的缩略图(解决OutOfMemoryError)内存溢出的Bug
由于android获取图片过大是会出现内存溢出的Bug 07-02 05:10:13.792: E/AndroidRuntime(6016): java.lang.OutOfMemoryError 解 ...
- android调用系统相机并获取图片
如果不是特别的要求,通过拍照的方式取得图片的话,我们一般调用系统的拍照来完成这项工作,而没必要再自己去实现一个拍照功能.调用系统相机很简单,只需要一个intent就可以跳转到相几界面,然后再通过onA ...
- Android 从Gallery获取图片
本文主要介绍Android中从Gallery获取图片 设计项目布局 <LinearLayout xmlns:android="http://schemas.android.com/ap ...
- Android 拍照或者从相册获取图片的实现
我们常常会用到上传头像,或者发帖子的时候选择本地图片上传的功能.这个很常见 今天因为app的需求我研究了下.现在分享下. 其实不论是通过拍照还是从相册选取都会用到Intent 这是系统提供给我们用来调 ...
随机推荐
- 分布式系统ID生成方案汇总
在分布式系统中,需要对大量的数据.消息.请求等进行唯一的标识,例如分布式数据库的ID需要满足唯一且多数据库同步,在单一系统中,使用数据库自增主键可以满足需求,但是在分布式系统中就需要一个能够生成全局唯 ...
- nginx处理HTTP header问题
在实际开发中遇到http header 自定义key中包含下划线(_)时服务端header丢失的问题,解决办法详细见以下网页内容,感谢原博主 http://blog.csdn.net/dac55300 ...
- April 28 2017 Week 17 Friday
The only thing more painful than learning from experience is not learning from experience. 比从经验中学习更为 ...
- sublime打开txt文件乱码的问题
我们使用Sublime打开TXT文件的时候,会经常因为编码的问题造成乱码. 这是因为TXT记事本的默认保存编码格式是GBK,而Sublime text不支持GB2312和GBK编码. 我们可以通过安装 ...
- IOS 线程的总结(及cell的图片下载)
零.线程的注意点(掌握) 1.不要同时开太多的线程(1~3条线程即可,不要超过5条)2.线程概念1> 主线程 : UI线程,显示.刷新UI界面,处理UI控件的事件2> 子线程 : 后台线程 ...
- Rich feature hierarchies for accurate object detection and semantic segmentation(RCNN)
https://zhuanlan.zhihu.com/p/23006190?refer=xiaoleimlnote http://blog.csdn.net/bea_tree/article/deta ...
- caffe中protobuf问题
安装caffe时,protobuf在使用import caffe时,python版的会提示protobuf的问题,原因是因为protobuf的安装是用python-proto,而在安装anaconda ...
- MySQL存储引擎MyISAM与InnoDB
一. MySQL存储引擎MyISAM与InnoDB如何选择 MySQL有多种存储引擎,每种存储引擎有各自的优缺点,可以择优选择使用:MyISAM.InnoDB.MERGE.MEMORY(HEAP).B ...
- PHP:(一)安装并使用PHP
php的安装分为两个部分:环境安装配置和开发工具 一.集成环境安装配置 (一)安装 选择:wampserver或者xampp 我采用的是xampp. 在http://www.sourceforce.n ...
- v-if
vue中通过v-if,v-else-if,v-else的对应的Boolean值来操作元素在dom中是否移除. 这里就以单纯的true,false来模拟一下.注:标签属性去出来的值为string类型. ...