public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
switch (v.getId()) {
case R.id.btFirst: startActivityForResult(intent, 1); break;
case R.id.btSecond: startActivityForResult(intent, 2); break;
case R.id.btDetect:
final String firstPath = etFirst.getText().toString();
final String secondPath = etSecond.getText().toString();

然后重写方法,对应:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (data != null) {
Cursor cursor = getContentResolver().query(data.getData(), null,
null, null, null);
cursor.moveToFirst();
int idx = cursor.getColumnIndex(ImageColumns.DATA);
String fileSrc = cursor.getString(idx);
if (requestCode == 1) {
etFirst.setText(fileSrc); } else if (requestCode == 2) {
etSecond.setText(fileSrc); }
cursor.close(); }
}

Cursor cursor得到数据库指针 cursor,指向data.getData,像是一条条存储,cursor.moveToFirst();指向存储的第一条,int idx = cursor.getColumnIndex(ImageColumns.DATA);idx相当一个索引,指向(ImageColumns.DATA,

requestCode很重要,startActivityForResult(Intent intent, int requestCode)中startActivityForResult中两个参数对应的requestCode,

http://blog.csdn.net/chenzheng_java/article/details/6266135

代码中:Intent intent = new Intent(Intent.ACTION_PICK);

android之Itent.ACTION_PICK  Intent.ACTION_GET_CONTENT妙用

你是不是很多时候,想从弹出的电话本姓名列表中中查找到某个人,然后再获取该人的详细信息呢?

你是不是想选择从弹出的列表中选择一张图片,然后将其进行进一步的操作呢?

如果,你想,那你是不是很像知道,我们应该怎么让其弹出来一张选择列表,又应该怎么代码实现后边的操作呢?

Itent.ACTION_PICK  Intent.ACTION_GET_CONTENT 两者都可以完成类似的功能,让我们一起来看下例子

第一:Intent.ACTION_PICK

首先添加一个权限: <uses-permission android:name="android.permission.READ_CONTACTS"/>
发起一个 Contact Picker
Intent intent = new Intent(Intent.ACTION_PICK, People.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
然后:
重写方法
@Override
public void onActivityResult(int reqCode, int resultCode, Intent data)
{
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode) {
case (PICK_CONTACT) :
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor c = managedQuery(contactData, null, null, null, null);
if (c.moveToFirst()) {
String name = c.getString(c.getColumnIndexOrThrow(People.NAME));
// TODO Whatever you want to do with the selected contact name.
}
} break; }
}

其中cursor是数据库的知识,相当于数据库中的指针,query得到contactdata,
例如

String[] columns = new String[] {People.NAME};
int[] names = new int[] {R.id.row_entry};
mAdapter = new SimpleCursorAdapter(this, R.layout.mycontacts, C, columns, names);
setListAdapter(mAdapter);
第二:Intent.ACTION_GET_CONTENT
 
我们可以发现,其实action_get_content是通过intent中设置的type属性来判断具体调用哪个程序的。
  1. Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
  2. intent.setType("audio/*");
  3. startActivity(Intent.createChooser(intent, "Select music"));
  1. <span style="font-family: comic sans ms,sans-serif;"><span style="font-size: x-small;">Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
  2. intent.setType("audio/*");
  3. startActivity(Intent.createChooser(intent, "Select music"));</span></span>
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("audio/*");
startActivity(Intent.createChooser(intent, "Select music"));

执行之 会弹出一个对话框 效果为:

其实 对于这段代码 大家应该都能猜出什么意思  现自己模拟并理解

[代码]

1. 定义TestActivity 用于根据传入Uri  播放目标

<span style="font-family: comic sans ms,sans-serif;"><span style="font-size: x-small;">public class TestActivity extends Activity {

    @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.setTitle("TestActivity"); Intent i = this.getIntent(); Uri u = i.getData(); try {
playMusic(u);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} public void playMusic(Uri uri) throws IllegalArgumentException, SecurityException, IllegalStateException, IOException{
MediaPlayer mp = new MediaPlayer();
mp.setDataSource(this, uri);
mp.prepare();
mp.start();
}
}</span></span>

2. 在AndroidManifest 注册TestActivity

<span style="font-family: comic sans ms,sans-serif;"><span style="font-size: x-small;"><activity android:name=".TestActivity"
android:label="TestActivity">
<intent-filter>
<action android:name="android.intent.action.GET_CONTENT" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.OPENABLE" />
<data android:mimeType="audio/music1" />
</intent-filter>
</activity></span></span>

3. 使用TestActivity

<span style="font-family: comic sans ms,sans-serif;"><span style="font-size: x-small;">public void sendChooser(){
Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setDataAndType(Uri.parse("file:///sdcard/DCIM/cc.mp3"), "audio/music1"); startActivity(Intent.createChooser(intent, "Select music1 app"));
}</span></span>

4. emulator 运行截图:

此外:

   //选择图片 requestCode 返回的标识

  Intent innerIntent = new Intent(Intent.ACTION_GET_CONTENT); //"android.intent.action.GET_CONTENT"

  innerIntent.setType(contentType); //查看类型 String IMAGE_UNSPECIFIED = "image/*";

  Intent wrapperIntent = Intent.createChooser(innerIntent, null);

  ((Activity) context).startActivityForResult(wrapperIntent, requestCode);

  //视频

  Intent innerIntent = new Intent(Intent.ACTION_GET_CONTENT);

  innerIntent.setType(contentType); //String VIDEO_UNSPECIFIED = "video/*";

  Intent wrapperIntent = Intent.createChooser(innerIntent, null);

  ((Activity) context).startActivityForResult(wrapperIntent, requestCode);

  //添加音频

  Intent innerIntent = new Intent(Intent.ACTION_GET_CONTENT);

  innerIntent.setType(contentType); //String VIDEO_UNSPECIFIED = "video/*";

  Intent wrapperIntent = Intent.createChooser(innerIntent, null);

  ((Activity) context).startActivityForResult(wrapperIntent, requestCode);

  //录音

  Intent intent = new Intent(Intent.ACTION_GET_CONTENT);

  intent.setType(ContentType.AUDIO_AMR); //String AUDIO_AMR = "audio/amr";

  intent.setClassName("com.android.soundrecorder",

  "com.android.soundrecorder.SoundRecorder");

  ((Activity) context).startActivityForResult(intent, requestCode);

  //拍摄视频

  int durationLimit = getVideoCaptureDurationLimit(); //SystemProperties.getInt("ro.media.enc.lprof.duration", 60);

  Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);

  intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0);

  intent.putExtra(MediaStore.EXTRA_SIZE_LIMIT, sizeLimit);

  intent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, durationLimit);

  startActivityForResult(intent, REQUEST_CODE_TAKE_VIDEO);

  //拍照 REQUEST_CODE_TAKE_PICTURE 为返回的标识

  Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); //"android.media.action.IMAGE_CAPTURE";

  intent.putExtra(MediaStore.EXTRA_OUTPUT, Mms.ScrapSpace.CONTENT_URI); // output,Uri.parse("content://mms/scrapSpace");

  startActivityForResult(intent, REQUEST_CODE_TAKE_PICTURE);

android脚步---Itent.ACTION_PICK ,startActivityForResult的更多相关文章

  1. android之Itent.ACTION_PICK Intent.ACTION_GET_CONTENT妙用

    你是不是很多时候,想从弹出的电话本姓名列表中中查找到某个人,然后再获取该人的详细信息呢? 你是不是想选择从弹出的列表中选择一张图片,然后将其进行进一步的操作呢? 如果,你想,那你是不是很像知道,我们应 ...

  2. android脚步---UI界面修改,关于activity中增加按钮和监听

    增加按钮和监听,这个和上个不同在于,它不是在一个dialog里面,而是从新写了一个activity,因此需要先找到这个activity的入口. case R.id.checkframe: if (mC ...

  3. android脚步---UI界面修改,增加按钮和监听

    我的UU界面,其布局如下: 需要修改的部分: 意见反馈居中,还有增加backbutton 首先在mainactivity中找到我的UU的定义:dialogue public void showAbou ...

  4. [Android] ACTION_GET_CONTENT与ACTION_PICK的区别

    参考:AndroidSDK/docs/reference/android/content/Intent 看到相关代码,对这两个参数的区别有点搞不清. 原文地址请保留http://www.cnblogs ...

  5. Android Exception 5(startActivityForResult & singleTask)

    昨天碰到一个很诧异的事情,主要内容是1.startActivityForResult(intent2, 302);2.onActivityResult 问题是:onActivityResult调用时间 ...

  6. 【Android初级】利用startActivityForResult返回数据到前一个Activity(附源码+解析)

    在Android里面,从一个Activity跳转到另一个Activity.再返回,前一个Activity默认是能够保存数据和状态的.但这次我想通过利用startActivityForResult达到相 ...

  7. android脚步--Relativelayout设置

    引自http://blog.csdn.net/lamp_zy/article/details/8035161 http://my.oschina.net/honeyming/blog/130761 以 ...

  8. android脚步---将layout和其输出一起保存成图片

    public void convertViewToBitmap(View view) { //View view = getLayoutInflater().inflate(R.layout.test ...

  9. android脚步---设置layout隐藏属性

    设置layout的属性,应用到android view的setVisibility 有三个值 visibility  VISIBLE, INVISIBLE, GONE. 可见的     不可见的    ...

随机推荐

  1. 如何使用kaptcha验证码组件

    kaptcha是基于SimpleCaptcha的验证码开源项目. kaptcha是纯配置的,使用起来比较友好.如使用了Servlet,所有配置都在web.xml中.如果你在项目中使用了开源框架(比如S ...

  2. VoIP的话音质量测量方法

    严重的呼叫质量和性能管理问题会影响VoIP (Voice over IP)系统的运作.网络管理员等人需要理解基本的呼叫质量测量技术才能很好地监测.管理和诊断在VoIP中出现的这些问题.本文介绍了常用的 ...

  3. Chapter 1 Mr.Sherlock Holmes

    I took my degree of Doctor of Medicine in the University of London, and proceeded to Netley to go th ...

  4. 第十一节,编辑器软件PyCharm 5.0.3

    编辑器软件PyCharm 5.0.3 设置 主题方案 字体大小 行距 文件模板 文件编码 版本切换

  5. url操作一网打尽(一)

    1:url实际应用简介 近期研究发现通过url传递参数很普遍的(淘宝也是这样做的), 通过修改url来传递参数,比如通过关键字搜索某件商品的时候,链接便追加了相应参数.在请求接口的时候直接对url进行 ...

  6. Broken Keyboard(悲剧文本)

    你有一个键盘,键盘上所有的键都能正常使用,只是Home键和End键有时会自动按下.你并不知道这一情况,而是专心地打稿子,甚至连显示器都没开电源.当你打开显示器之后,展现在你面前的是一段悲剧文本.你的任 ...

  7. OpenCV ——双线性插值(Bilinear interpolation)

    1,原理 在图像的仿射变换中,很多地方需要用到插值运算,常见的插值运算包括最邻近插值,双线性插值,双三次插值,兰索思插值等方法,OpenCV提供了很多方法,其中,双线性插值由于折中的插值效果和运算速度 ...

  8. 找最大重复次数的数和重复次数(C++ Pair)

    Problem A: 第一集 你好,世界冠军 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 265  Solved: 50[Submit][Statu ...

  9. 2.请尝试安装和配置JDK,并给出安装、配置JDK的步骤。

    win10/64位 1.解压jdk1.8.0_91_x64.rar 2.同时按住win键和pause键,弹出系统属性窗口,选择高级系统设计选项. 3.然后单击环境变量按钮. 4.弹出环境变量对话框后, ...

  10. td里的内容宽度自适应 及 鼠标放上显示标题div title

    td里的内容自适应宽度, 用 width:100%控制 strRight+="<td bordercolor='#DEDEDE' width='500px' height='50px' ...