`
ruirui516888
  • 浏览: 137781 次
文章分类
社区版块
存档分类
最新评论

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;

}
}

例如
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. Intentintent=newIntent(Intent.ACTION_GET_CONTENT);
  2. intent.setType("audio/*");
  3. startActivity(Intent.createChooser(intent,"Selectmusic"));

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

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

[代码]

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

Java代码
  1. publicclassTestActivityextendsActivity{
  2. @Override
  3. publicvoidonCreate(BundlesavedInstanceState){
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.main);
  6. this.setTitle("TestActivity");
  7. Intenti=this.getIntent();
  8. Uriu=i.getData();
  9. try{
  10. playMusic(u);
  11. }catch(IllegalArgumentExceptione){
  12. //TODOAuto-generatedcatchblock
  13. e.printStackTrace();
  14. }catch(SecurityExceptione){
  15. //TODOAuto-generatedcatchblock
  16. e.printStackTrace();
  17. }catch(IllegalStateExceptione){
  18. //TODOAuto-generatedcatchblock
  19. e.printStackTrace();
  20. }catch(IOExceptione){
  21. //TODOAuto-generatedcatchblock
  22. e.printStackTrace();
  23. }
  24. }
  25. publicvoidplayMusic(Uriuri)throwsIllegalArgumentException,SecurityException,IllegalStateException,IOException{
  26. MediaPlayermp=newMediaPlayer();
  27. mp.setDataSource(this,uri);
  28. mp.prepare();
  29. mp.start();
  30. }
  31. }

2. 在AndroidManifest 注册TestActivity

Java代码
  1. <activityandroid:name=".TestActivity"
  2. android:label="TestActivity">
  3. <intent-filter>
  4. <actionandroid:name="android.intent.action.GET_CONTENT"/>
  5. <categoryandroid:name="android.intent.category.DEFAULT"/>
  6. <categoryandroid:name="android.intent.category.OPENABLE"/>
  7. <dataandroid:mimeType="audio/music1"/>
  8. </intent-filter>
  9. </activity>

3. 使用TestActivity

Java代码
  1. publicvoidsendChooser(){
  2. Intentintent=newIntent(Intent.ACTION_GET_CONTENT);
  3. intent.setDataAndType(Uri.parse("file:///sdcard/DCIM/cc.mp3"),"audio/music1");
  4. startActivity(Intent.createChooser(intent,"Selectmusic1app"));
  5. }

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代码-基于 Bottom Sheet 实现的图片选择器,交互效果不错。

    What is TedBottomPicker? In Google's Material Design, Google introduce Bottom ...Get image from gallery(using Intent.ACTION_PICK intent) Get image from recent image(using MediaStore.Images.Media.EXTE

    Android实现图片自动轮换

    intent.setAction(Intent.ACTION_PICK); startActivityForResult(intent, REQUEST_PICTURE_CHOOSE); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { ...

    Android4.4 WebAPI实现拍照上传功能

    网上有很多关于拍照上传的实现方法,如果用新版本android去运行有可能会发现根本实现不了。...最简单的解决办法是用intent.ACTION_PICK代替intent.ACTION_GET_CONTENT。 下面给出4.4版本后拍照上传的具体实现方法: 

    新版Android开发教程.rar

    的 Android SDK 提供了在 Android 平台上使用 JaVa 语言进行 Android 应用开发必须的工具和 API 接口。 特性 • 应用程序框架 支持组件的重用与替换 • Dalvik Dalvik Dalvik Dalvik 虚拟机 专为移动设备优化 • ...

    自写的Android直接发送短信的方法.rar

      Intent intent = new Intent(Intent.ACTION_PICK, uri);//创建Intent   startActivityForResult(intent, 1);//切换到通讯录  }  else if(v == send){//按下发送按钮   v.setEnabled(false);//设置按钮为不...

    android打开本地图像的方法

    intent.setAction(Intent.ACTION_GET_CONTENT); startActivityForResult(intent, 1); 方法二,调用手机自身图像浏览工具浏览: Intent intent = new Intent( Intent.ACTION_PICK, android.provider.MediaStore

    react-native-document-picker:使用文档提供程序的React Native的文档选择器

    Android的Intent.ACTION_GET_CONTENT Windows Windows.Storage.Pickers 需要Android 5.0以上版本和iOS 10以上版本 安装 npm i --save react-native-document-picker OR yarn add react-native-document-picker ...

    Google Android SDK开发范例大全(PDF高清完整版1)(4-1)

    7.6 取得手机内置媒体里的图文件——ACTION_GET_CONTENT取回InputStream 7.7 相片导航向导与设置背景桌面——ImageSwitcher与Gallery 7.8 调整音量大小声——AudioManager控制音量 7.9 播放mp3资源文件——raw文件夹...

    Google Android SDK开发范例大全(PDF完整版4)(4-4)

    7.6 取得手机内置媒体里的图文件——ACTION_GET_CONTENT取回InputStream 7.7 相片导航向导与设置背景桌面——ImageSwitcher与Gallery 7.8 调整音量大小声——AudioManager控制音量 7.9 播放mp3资源文件——raw文件夹...

    Google Android SDK开发范例大全(PDF高清完整版3)(4-3)

    7.6 取得手机内置媒体里的图文件——ACTION_GET_CONTENT取回InputStream 7.7 相片导航向导与设置背景桌面——ImageSwitcher与Gallery 7.8 调整音量大小声——AudioManager控制音量 7.9 播放mp3资源文件——raw文件夹...

    Google Android SDK开发范例大全(完整版附部分源码).pdf

    7.6 取得手机内置媒体里的图文件——ACTION_GET_CONTENT取回InputStream 7.7 相片导航向导与设置背景桌面——ImageSwitcher与Gallery 7.8 调整音量大小声——AudioManager控制音量 7.9 播放mp3资源文件——raw...

    Google Android SDK开发范例大全的目录

    7.6 取得手机内置媒体里的图文件——ACTION_GET_CONTENT取回InputStream 7.7 相片导航向导与设置背景桌面——ImageSwitcher与Gallery 7.8 调整音量大小声——AudioManager控制音量 7.9 播放mp3资源文件——raw文件夹...

    Google Android SDK 开发范例大全01

    7.6 取得手机内置媒体里的图文件——ACTION_GET_CONTENT取回InputStream 7.7 相片导航向导与设置背景桌面——ImageSwitcher与Gallery 7.8 调整音量大小声——AudioManager控制音量 7.9 播放mp3资源文件——raw文件夹...

    Google Android SDK 开发范例大全02

    7.6 取得手机内置媒体里的图文件——ACTION_GET_CONTENT取回InputStream 7.7 相片导航向导与设置背景桌面——ImageSwitcher与Gallery 7.8 调整音量大小声——AudioManager控制音量 7.9 播放mp3资源文件——raw文件夹...

    Google+Android+SDK开发范例大全

    7.4 用手指移动画面里的照片——onTouchEvent事件判断 7.5 加载存储卡的Gallery相簿——FileArrayList 7.6 取得手机内置媒体里的图文件——ACTION_GET_CONTENT取回InputStream 7.7 相片导航向导与设置背景桌面——...

    Google Android sdk 开发范例大全 部分章节代码

    7.6 取得手机内置媒体里的图文件——ACTION_GET_CONTENT取回InputStream 7.7 相片导航向导与设置背景桌面——ImageSwitcher与Gallery 7.8 调整音量大小声——AudioManager控制音量 7.9 播放mp3资源文件——raw文件夹...

    Google Android SDK开发范例大全(完整版)

    7.6 取得手机内置媒体里的图文件——ACTION_GET_CONTENT取回InputStream 7.7 相片导航向导与设置背景桌面——ImageSwitcher与Gallery 7.8 调整音量大小声——AudioManager控制音量 7.9 播放mp3资源文件——raw文件夹...

    精通ANDROID 3(中文版)1/2

    5.4 练习使用ACTION_PICK  5.5 练习使用GET_CONTENT操作  5.6 挂起的Intent  5.7 资源  5.8 小结  第6章 构建用户界面和使用控件  6.1 Android中的UI开发  6.1.1 完全利用代码来构建UI  6.1.2 完全...

Global site tag (gtag.js) - Google Analytics