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, 在弹出的 ...
随机推荐
- virtualenv虚拟环境安装不同版本的django
在开发Python应用程序的时候,系统安装的Python3只有一个版本:3.4.所有第三方的包都会被pip安装到Python3的site-packages目录下. 如果我们要同时开发多个应用程序,那这 ...
- BZOJ.2738.矩阵乘法(整体二分 二维树状数组)
题目链接 BZOJ 洛谷 整体二分.把求序列第K小的树状数组改成二维树状数组就行了. 初始答案区间有点大,离散化一下. 因为这题是一开始给点,之后询问,so可以先处理该区间值在l~mid的修改,再处理 ...
- Codeforces Round #463
A - Palindromic Supersequence /* 题目大意:给出一个串,构造一个包含该串的回文串 */ #include <cstdio> #include <alg ...
- python学习两月总结_汇总大牛们的思想_值得收藏
下面是我汇总的我学习两个月python(version:3.3.2)的所有笔记 你可以访问:http://www.python.org获取更多信息 你也可以访问:http://www.cnblogs. ...
- UVALive 6661 Equal Sum Sets
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...
- spring---aop(9)---Spring AOP中引入增强
写在前面 Spring将introduction通知看作一种特殊类型的拦截通知.用Spring的行话来讲,对方法的增强叫做Wearing(织入),而对类的增强叫introduction(引入).Int ...
- DOM事件绑定方式
普通事件可以直接绑定 比如document.onmouseover=fn; 或者document.addEventListener("mouseover",fn,flase); a ...
- PHPExcel 导出2003和2007的excel文档实例
require_once 'common/excel/PHPExcel.php'; require_once 'common/excel/phpExcel/Writer/Excel2007.php'; ...
- MVC使用TempData跨控制器传递信息而无需记住key的名称
通常情况下,使用TempData需要记住key的名称,本篇体验:通过帮助类,实现对TempData的设置.获取.删除. 关于传递信息的类: namespace MvcApplication1.Mode ...
- Android实例剖析笔记(一)
摘要:用实例讲解Andriod的开发过程 开卷语 俗话说,“熟读唐诗三百首,不会作诗也会吟”.最近收集了很多Android的示例代码,从这些代码的阅读和实验中学习到很多知识,从而产生写这个系列的打算, ...