Android -- 打开本地图片且显示路径
背景



代码
先上布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/hello_world"/>
<ImageView
android:id="@+id/pic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:contentDescription="@string/app_name"/>
</LinearLayout>
这里没有TextView,我最后是将路径以System.out.println方式输出的。
初始化:
button = (Button)findViewById(R.id.button);
pic = (ImageView) findViewById(R.id.pic);
button.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
// TODO 自动生成的方法存根
System.out.println("onClick");
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, 1);
}
});
对于startActivityForResult的回调函数进行覆写:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO 自动生成的方法存根
System.out.println(requestCode+"");
if(requestCode==1)
{
//获得图片的uri
Uri uri = data.getData();
//外界的程序访问ContentProvider所提供数据 可以通过ContentResolver接口
ContentResolver cr = this.getContentResolver();
Bitmap bitmap;
//Bitmap bm; //这是一种方式去读取图片
try
{
//bm = MediaStore.Images.Media.getBitmap(cr, uri);
//pic.setImageBitmap(bm);
bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri));
pic.setImageBitmap(bitmap);
System.out.println("GOOD");
//第一种方式去读取路径
//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);
System.out.println(path);
*/
//第二种方式去读取路径
Cursor cursor =this.getContentResolver().query(uri, null, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String path = cursor.getString(column_index);
System.out.println(path);
}
catch (Exception e)
{
// TODO 自动生成的 catch 块
e.printStackTrace();
System.out.println("BAD");
} }
super.onActivityResult(requestCode, resultCode, data);
}
最后的path就是路径。
我是天王盖地虎的分割线
源代码:http://pan.baidu.com/s/1dD1Qx01
OpenPic.zip
转载请注明出处:http://www.cnblogs.com/yydcdut/p/3720594.html
Android -- 打开本地图片且显示路径的更多相关文章
- android获取本地图片并显示图片
import java.io.FileNotFoundException; import android.content.ContentResolver; import android.content ...
- Springboot读取本地图片并显示
在application.yml中配置url访问路径和本地图片路径: 方框1:url中访问路径,这里为:localhost:8080/testspringboot/image/... 方框2:本地图片 ...
- js读取本地图片并显示
抄自 http://blog.csdn.net/qiulei_21/article/details/52785191 js读取本地图片并显示 第一种方法比较好 版权声明:本文为博主原创文章,未经博主允 ...
- Android实现本地图片选择及预览缩放效果仿春雨医生
在做项目时常常会遇到选择本地图片的需求.曾经都是懒得写直接调用系统方法来选择图片.可是这样并不能实现多选效果.近期又遇到了,所以还是写一个demo好了.以后也方便使用.还是首先来看看效果 显示的图片使 ...
- 怪胎:Android开发ImageView图片无法显示
今天碰到一个非常奇怪的问题: 在Android中ImageView无法显示加载的本地SDCard图片. 具体过程是:先调用本地照相机程序摄像,然后将拍摄的图片加载在ImageView中显示. publ ...
- android -------- 打开本地浏览器或指定浏览器加载,打电话,打开第三方app
开发中常常有打开本地浏览器加载url或者指定浏览器加载, 还有打开第三方app, 如 打开高德地图 百度地图等 在Android程序中我们可以通过发送隐式Intent来启动系统默认的浏览器. 如果手机 ...
- 🍓 vue循环渲染本地图片不显示? 🍓
teamList: [{ title: '大数据拍牌', imgUrl: './img/data.jpg', introduce: '5星服务:强烈推荐', cost: '15000', bail: ...
- 实时显示从file输入框中打开的图片C:\fakepath路径问题
html代码: <input id="file_upload" type="file" /> <div class="image_c ...
- 19-Javaweb项目读取本地图片通过虚拟路径
有时会把文件存在本地如将图片等放在c.d盘等,在javaweb引用时会出现无法直接访问的问题,但是还是有办法解决的. 可以通过配置虚拟路径: 步骤一: 双击servers下面的tomcat, 在弹出的 ...
随机推荐
- http远程调用原生get、post模板
一.get方法 package lq.httpclient.method; import java.io.BufferedReader; import java.io.IOException; imp ...
- 时间插件datetimepicker
相关datetimepicker用法查看官网http://eonasdan.github.io/bootstrap-datetimepicker/ {% load staticfiles %} < ...
- collection 和 collections
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha collection 是集合的意思. 集合 是 集合类的上级接口, 比如 set 和 l ...
- webwork或Struts配置网站根路径的默认页面办法
参考资料:http://www.iteye.com/problems/24028 查阅好多资料,关于webwork或Struts处理默认页面的方式,能否像spring MVC那样直接指定默认访问页面. ...
- 浙江省队选拔 ZJOI2015 (Round 1) 解题报告
最近莫名其妙地喜欢上了用这种格式写各省省选的全套题解= = 今年浙江省选的出题人是算法竞赛界传说级人物陈立杰,看样子他的出题风格很有特点……ABC三题难度是严格递减的,感觉如果在做第一题的时候被卡住的 ...
- lua协程----ngx-lua线程学习笔记
--[[ - @desc lua数据输出 - @param string 字符串 - return string --]] function dump(v) if not __dump then fu ...
- Codeforces Round #228 (Div. 1) C. Fox and Card Game 博弈
C. Fox and Card Game 题目连接: http://codeforces.com/contest/388/problem/C Description Fox Ciel is playi ...
- CodeForces 32C. Flea 水题
C. Flea time limit per test 2 seconds memory limit per test 256 megabytes input standard input outpu ...
- PAT甲级1123. Is It a Complete AVL Tree
PAT甲级1123. Is It a Complete AVL Tree 题意: 在AVL树中,任何节点的两个子树的高度最多有一个;如果在任何时候它们不同于一个,则重新平衡来恢复此属性.图1-4说明了 ...
- This seems to be a pre-built javascript file. webpack报这个警告怎么办?
增加 module.noParse 进行解决 例如: { resolve: { alias: { 'react': 'my/react/path' } }, module: { noParse: [/ ...