Android学习笔记进阶十三获得本地全部照片
这是Intent的一个用法。
在ActivityAction里面有一个“ACTION_GET_CONTENT”字符串常量,该常量让用户选择特定类型的数据。
intent.setType("image/*"); 选择本地所有的图片。
返回该数据的URI.我们利用该常量生成该图片的位图Bitmap,然后为添加到图片控件(ImageView)上就行了。

选择你想要的图片:

main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <Button
- android:id="@+id/button"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- <ImageView
- android:id="@+id/image"
- android:layout_width="fill_parent"
- android:scaleType="fitXY"
- android:layout_height="wrap_content"
- />
- </LinearLayout>
- package xiaosi.image;
- import java.io.FileNotFoundException;
- import android.app.Activity;
- import android.content.ContentResolver;
- import android.content.Intent;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.net.Uri;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.ImageView;
- public class ImageActivity extends Activity {
- /** Called when the activity is first created. */
- private Button button = null;
- private ImageView imageView = null;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- button = (Button)findViewById(R.id.button);
- button.setText("选择图片");
- button.setOnClickListener(new ButtonListener());
- }
- private class ButtonListener implements OnClickListener{
- public void onClick(View v)
- {
- Intent intent = new Intent();
- /* 开启Pictures画面Type设定为image */
- intent.setType("image/*");
- /* 使用Intent.ACTION_GET_CONTENT这个Action */
- intent.setAction(Intent.ACTION_GET_CONTENT);
- /* 取得相片后返回本画面 */
- startActivityForResult(intent, 1);
- }
- }
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- if (resultCode == RESULT_OK) {
- Uri uri = data.getData();
- Log.e("uri", uri.toString());
- ContentResolver contentResolver = this.getContentResolver();
- try {
- Bitmap bitmap = BitmapFactory.decodeStream(contentResolver.openInputStream(uri));
- imageView = (ImageView) findViewById(R.id.image);
- /* 将Bitmap设定到ImageView */
- imageView.setImageBitmap(bitmap);
- }
- catch (FileNotFoundException e){
- Log.e("Exception", e.getMessage(),e);
- }
- }
- super.onActivityResult(requestCode, resultCode, data);
- }
- }
源代码:点击打开链接
Android学习笔记进阶十三获得本地全部照片的更多相关文章
- Android学习笔记进阶之在图片上涂鸦(能清屏)
Android学习笔记进阶之在图片上涂鸦(能清屏) 2013-11-19 10:52 117人阅读 评论(0) 收藏 举报 HandWritingActivity.java package xiaos ...
- Android学习笔记进阶17之LinearGradient
具体的看一下博文:Android学习笔记进阶15之Shader渲染 package xiaosi.BitmapShader; import android.app.Activity; import a ...
- Android学习笔记进阶16之BitmapShader
<1>简介 具体的看一下博文:Android学习笔记进阶15之Shader渲染 public BitmapShader(Bitmap bitmap,Shader.TileMode ti ...
- Android学习笔记进阶18 之画图并保存图片到本地
1.首先创建一个Bitmap图片,并指定大小: 2.在该图片上创建一个新的画布Canvas,然后在画布上绘制,并保存即可: 3.需要保存的目录File,注意如果写的目录如“/sdcard/so ...
- Android学习笔记进阶18之画图并保存图片到本地
1.首先创建一个Bitmap图片,并指定大小: 2.在该图片上创建一个新的画布Canvas,然后在画布上绘制,并保存即可: 3.需要保存的目录File,注意如果写的目录如“/sdcard/so ...
- 【转】Pro Android学习笔记(十三):用户界面和控制(1):UI开发
目录(?)[-] UI开发 方式一通过XML文件 方式二通过代码 方式三XML代码 UI开发 先理清一些UI概念: view.widget.control:这三个名词其实没有什么区别,都是一个UI元素 ...
- Android学习笔记(十三)——广播机制
//此系列博文是<第一行Android代码>的学习笔记,如有错漏,欢迎指正! Android 中的每个应用程序都可以对自己感兴趣的广播进行注册,这样该程序就只会接收到自己所关心的广播内容 ...
- Android学习笔记(十三)SharedPreference必须掌握的基础
我们在开发中,应用程序会保存少量数据,例如一些字符串.一些标记或者一些配置文件,这时候如果去使用SQLite保存这些数据的话,难免会显得大材小用,用起来也不方便,对于这种信息,保存在SharedPre ...
- Android学习笔记(十三) Handler
可用于解决上一则笔记所提到的WorkerThread无法修改UI控件的问题 一.Handler.Looper和MessageQueue的基本原理 Handler把消息对象放到MessageQueue当 ...
随机推荐
- springboot实现热部署,修改代码不用重启服务
1.引入热部署依赖 <!-- 热部署模块 --> <dependency> <groupId>org.springframework.boot</groupI ...
- DOM基础及DOM操作HTML
文件对象模型(Document Object Model.简称fr=aladdin" target="_blank">DOM).是W3C组织推荐的处理可扩展标 ...
- iOS开发 - 数据归档与恢复 NSKeyedArchiver
归档与恢复归档 归档,英文Archiver['ɑrkɪvə],这里指的是将OC的对象存储为一个文件或者网络上的一个数据块. 恢复归档.英文UnArchiver,指的是将一个来自文件或网络的归档数据块恢 ...
- 【LeetCode-面试算法经典-Java实现】【032-Longest Valid Parentheses(最长有效括号)】
[032-Longest Valid Parentheses(最长有效括号)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a string contai ...
- android-Animation进阶(创造用户舒服的动画)
android中经常使用的动画有Animation ,Animator两种; ---第1种经常使用的是使用在Activity切换中.比方打开一个Activity.关闭一个Activity 个人比較喜欢 ...
- Mysql之索引的基本概念
一.索引是什么? 比如我们要在字典中找某一字,如何才能快速找到呢?那就是通过字典的目录. 对数据库来说,索引的作用就是给‘数据’加目录. 二.索引算法 设有N条随机记录,不用索引,平均查找N/2次,那 ...
- eclipse C开发添加自己的头文件搜索路径
eclipse编译C程序时提示: ..\src\main.c:8:21: fatal error: my_type.h: No such file or directory 如图: 需要添加自己的头文 ...
- 1.cocos_helloworld
在class HelloWorld : public cocos2d::Layer中添加函数 void menuclose(cocos2d::Ref *psender); 实现: void Hello ...
- 方便查看线程状况的jsp页面
此方法来自深入理解java虚拟机一书,用作管理员页面,可以随时用浏览器查看线程堆栈 <%@ page language="java" import="java.ut ...
- Server.UrlEncode与HttpUtility.UrlEncode的区别
一.HttpUtility.UrlEncode 方法 1.public static string UrlEncode(byte[]) 将字节数组转换为已编码的 URL 字符串. 2.public s ...