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 intent = new Intent(Intent.ACTION_GET_CONTENT);
- intent.setType("audio/*");
- startActivity(Intent.createChooser(intent, "Select music"));
- <span style="font-family: comic sans ms,sans-serif;"><span style="font-size: x-small;">Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
- intent.setType("audio/*");
- startActivity(Intent.createChooser(intent, "Select music"));</span></span>
执行之 会弹出一个对话框 效果为:
其实 对于这段代码 大家应该都能猜出什么意思 现自己模拟并理解之
[代码]
1. 定义TestActivity 用于根据传入Uri 播放目标
- 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 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
- <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 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
- 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 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 Intent.ACTION_GET_CONTENT妙用的更多相关文章
- android脚步---Itent.ACTION_PICK ,startActivityForResult
public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(Intent.A ...
- Android 开发之:Intent.createChooser() 妙用
大家对该功能第一印象就是ApiDemo 里面的 其只有区区几行代码 提取为: Intent intent = new Intent(Intent.ACTION_GET_CONTENT); inten ...
- android学习笔记29——Intent/IntentFilter
Intent/IntentFilter Intent封装android应用程序需要启动某个组件的“意图”,Intent还是应用程序组件之间通信的重要媒介. EG:Activity之间需要交换数据时,使 ...
- Android开发中使用Intent跳转到系统应用中的拨号界面、联系人界面、短信界面
现在开发中的功能需要直接跳转到拨号.联系人.短信界面等等,查找了很多资料,自己整理了一下. 首先,我们先看拨号界面,代码如下: Intent intent =new Intent(); intent. ...
- Android四大组件之Intent(续2)
1.你如何通过一个intent来唤醒activity? this.startActivity(intent,request); 2.什么是显式.隐式的intents? 显式:指定组件名,通常 ...
- Android开发之常用Intent.Action【转】
1.从google搜索内容 Intent intent = new Intent(); intent.setAction(Intent.ACTION_WEB_SEARCH); intent.putEx ...
- Android开发学习之Intent具体解释
Intent简单介绍和具体解释: Intent:协助应用间的交互与通信,Intent负责相应用中一次操作的动作.动作涉及的数据.附加数据进行描写叙述. ...
- (Android数据传递)Intent消息传递机制 “Intent”“数据传递”
Intent类的继承关系: 需要注意的是,该类实现了Parcelable(用于数据传递)和Cloneable接口. Intent是一种(系统级别的)消息传递机制,可以在应用程序内使用,也可以在应用 ...
- Android系统中标准Intent的使用
Android系统用于Activity的标准Intent 1.根据联系人ID显示联系人信息= Intent intent=new Intent(); intent.setAction(Intent.A ...
随机推荐
- WCF服务中,[DataMember]属性标记的属性一定要有set访问器
WCF服务中,如果实体类中,包含有[DataMember]属性标记时,该属性一定要有set访问器.当系统必须调用到[DataMember]标记的属性时,如果该属性没有set访问器,则会出错.
- ssh 远程 centos 乱码
今天,帮我们同学处理一下中文显示乱码的问题.这个是个国内Linux用户烦恼的问题,由于大部分的Linux发行版都是以英语为主体的,而且英文在通用性和稳定性上都比中文要好一些,各种奇怪的BUG也要少一点 ...
- Unity3D判断鼠标向右或向左滑动,响应不同的事件
private var first = Vector2.zero; private var second = Vector2.zero; function Update () { } function ...
- close和shutdown的区别
转的,没验证 close(sock_fd)会把sock_fd的内部计数器减1当sock_fd的内部计数器为0时, 才调用shutodwn(), 并最终释放文件描述符调用shutdown()只是进行了T ...
- C#&java重学笔记(变量与操作符)
声明:自用参看读物 C#部分 1.C#中的byte和sbyte并不是二进制的比特,而是无符号 和 有符号的 8位整数. 2.decimal和float double都用来表示小数,前者用e的10的几次 ...
- iOS 隐藏顶部状态栏方式和更改颜色
plist文件里面添加 AppDelegate: //显示状态栏 [[UIApplication sharedApplication]setStatusBarHidden:NO]; //将状态栏颜色设 ...
- POJ 2039
#include<iostream> #include<stdio.h> #include<string> #define MAXN 20 using namesp ...
- 自动化 测试框架部署(python3+selenium2)
安装Python 从https://www.python.org/downloads/下载最新版本的Python3,请注意,是3: 需要将Python的安装目录和安装目录下的Scripts文件夹添加到 ...
- junit中的assert方法总结
junit中的assert方法全部放在Assert类中,总结一下junit类中assert方法的分类.1.assertTrue/False([String message,]boolean condi ...
- 【poj1284-Primitive Roots】欧拉函数-奇素数的原根个数
http://poj.org/problem?id=1284 题意:给定一个奇素数p,求p的原根个数. 原根: { (xi mod p) | 1 <= i <= p-1 } is equa ...