这是Intent的一个用法。

在ActivityAction里面有一个“ACTION_GET_CONTENT”字符串常量,该常量让用户选择特定类型的数据。

intent.setType("image/*");  选择本地所有的图片。

返回该数据的URI.我们利用该常量生成该图片的位图Bitmap,然后为添加到图片控件(ImageView)上就行了。

选择你想要的图片:

main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <Button
  8. android:id="@+id/button"
  9. android:layout_width="fill_parent"
  10. android:layout_height="wrap_content"
  11. />
  12. <ImageView
  13. android:id="@+id/image"
  14. android:layout_width="fill_parent"
  15. android:scaleType="fitXY"
  16. android:layout_height="wrap_content"
  17. />
  18. </LinearLayout>
  1. package xiaosi.image;
  2. import java.io.FileNotFoundException;
  3. import android.app.Activity;
  4. import android.content.ContentResolver;
  5. import android.content.Intent;
  6. import android.graphics.Bitmap;
  7. import android.graphics.BitmapFactory;
  8. import android.net.Uri;
  9. import android.os.Bundle;
  10. import android.util.Log;
  11. import android.view.View;
  12. import android.view.View.OnClickListener;
  13. import android.widget.Button;
  14. import android.widget.ImageView;
  15. public class ImageActivity extends Activity {
  16. /** Called when the activity is first created. */
  17. private Button button = null;
  18. private ImageView imageView = null;
  19. @Override
  20. public void onCreate(Bundle savedInstanceState) {
  21. super.onCreate(savedInstanceState);
  22. setContentView(R.layout.main);
  23. button = (Button)findViewById(R.id.button);
  24. button.setText("选择图片");
  25. button.setOnClickListener(new ButtonListener());
  26. }
  27. private class ButtonListener implements OnClickListener{
  28. public void onClick(View v)
  29. {
  30. Intent intent = new Intent();
  31. /* 开启Pictures画面Type设定为image */
  32. intent.setType("image/*");
  33. /* 使用Intent.ACTION_GET_CONTENT这个Action */
  34. intent.setAction(Intent.ACTION_GET_CONTENT);
  35. /* 取得相片后返回本画面 */
  36. startActivityForResult(intent, 1);
  37. }
  38. }
  39. @Override
  40. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  41. if (resultCode == RESULT_OK) {
  42. Uri uri = data.getData();
  43. Log.e("uri", uri.toString());
  44. ContentResolver contentResolver = this.getContentResolver();
  45. try {
  46. Bitmap bitmap = BitmapFactory.decodeStream(contentResolver.openInputStream(uri));
  47. imageView = (ImageView) findViewById(R.id.image);
  48. /* 将Bitmap设定到ImageView */
  49. imageView.setImageBitmap(bitmap);
  50. }
  51. catch (FileNotFoundException e){
  52. Log.e("Exception", e.getMessage(),e);
  53. }
  54. }
  55. super.onActivityResult(requestCode, resultCode, data);
  56. }
  57. }

源代码:点击打开链接

Android学习笔记进阶十三获得本地全部照片的更多相关文章

  1. Android学习笔记进阶之在图片上涂鸦(能清屏)

    Android学习笔记进阶之在图片上涂鸦(能清屏) 2013-11-19 10:52 117人阅读 评论(0) 收藏 举报 HandWritingActivity.java package xiaos ...

  2. Android学习笔记进阶17之LinearGradient

    具体的看一下博文:Android学习笔记进阶15之Shader渲染 package xiaosi.BitmapShader; import android.app.Activity; import a ...

  3. Android学习笔记进阶16之BitmapShader

    <1>简介 具体的看一下博文:Android学习笔记进阶15之Shader渲染 public   BitmapShader(Bitmap bitmap,Shader.TileMode ti ...

  4. Android学习笔记进阶18 之画图并保存图片到本地

    1.首先创建一个Bitmap图片,并指定大小:   2.在该图片上创建一个新的画布Canvas,然后在画布上绘制,并保存即可:   3.需要保存的目录File,注意如果写的目录如“/sdcard/so ...

  5. Android学习笔记进阶18之画图并保存图片到本地

    1.首先创建一个Bitmap图片,并指定大小:   2.在该图片上创建一个新的画布Canvas,然后在画布上绘制,并保存即可:   3.需要保存的目录File,注意如果写的目录如“/sdcard/so ...

  6. 【转】Pro Android学习笔记(十三):用户界面和控制(1):UI开发

    目录(?)[-] UI开发 方式一通过XML文件 方式二通过代码 方式三XML代码 UI开发 先理清一些UI概念: view.widget.control:这三个名词其实没有什么区别,都是一个UI元素 ...

  7. Android学习笔记(十三)——广播机制

     //此系列博文是<第一行Android代码>的学习笔记,如有错漏,欢迎指正! Android 中的每个应用程序都可以对自己感兴趣的广播进行注册,这样该程序就只会接收到自己所关心的广播内容 ...

  8. Android学习笔记(十三)SharedPreference必须掌握的基础

    我们在开发中,应用程序会保存少量数据,例如一些字符串.一些标记或者一些配置文件,这时候如果去使用SQLite保存这些数据的话,难免会显得大材小用,用起来也不方便,对于这种信息,保存在SharedPre ...

  9. Android学习笔记(十三) Handler

    可用于解决上一则笔记所提到的WorkerThread无法修改UI控件的问题 一.Handler.Looper和MessageQueue的基本原理 Handler把消息对象放到MessageQueue当 ...

随机推荐

  1. 2014 CodingTrip - 携程编程大赛 (预赛第一场)

    1001: 可以证明(扩展欧几里得),只要卡片中有两个卡片互素,旁边点就是可达的. 因此只需要算出所有卡片不互素的情况有多少种,可用容斥原理. #include <cstdio> #inc ...

  2. 网络载入数据和解析JSON格式数据案例之空气质量监測应用

    一.创建一个新的项目 activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res ...

  3. Android设置背景图片平铺

    以LinearLayout为例,它提供的background属性将会将背景图片拉伸,相当难看.其实我们仅仅需做少量的改动就能够实现web编程中css背景图片的效果.来试试吧. 创建反复的背景图片 在d ...

  4. 【v2.x OGE教程 17】事务处理

    游戏代码中常常有些逻辑须要处理.因此OGE引擎加入了一个IUpdateHandler的类. IUpdateHandler类是OGE引擎中使用频率很之高的组件之中的一个,其本身是一个接口.内部有onUp ...

  5. Spark修炼之道(高级篇)——Spark源代码阅读:第十二节 Spark SQL 处理流程分析

    作者:周志湖 以下的代码演示了通过Case Class进行表Schema定义的样例: // sc is an existing SparkContext. val sqlContext = new o ...

  6. 父子间通信四 ($dispatch 和 $broadcast用法)

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. sql 向上取整 向下取整 四舍五入的实例;

    SELECT CEILING(23.5/4)'向上取整' ---6 :SELECT FLOOR(23.5/4)'向下取整' ---5 :SELECT ROUND(23.5/4,1)'四舍五入' --5 ...

  8. express随笔

    Express 是node 第三方框架,框架的意义就在于能大大简化程序地开发.看一下Express是怎么简化node程序开发的. 1,用Express写一个hello world 程序,我们来体验一下 ...

  9. PHP 相关配置

    1. php-fpm的pool 编辑"php-fpm"配置文件"php-fpm.con" vim /usr/local/php/etc/php-fpm.conf ...

  10. python note #2

    This passage will talk about some small but pretty important knowledge points, including using metho ...