android之Itent.ACTION_PICK Intent.ACTION_GET_CONTENT妙用
来源:互联网 发布:方维拼团源码下载 编辑:程序博客网 时间:2024/06/08 04:33
你是不是很多时候,想从弹出的电话本姓名列表中中查找到某个人,然后再获取该人的详细信息呢?
你是不是想选择从弹出的列表中选择一张图片,然后将其进行进一步的操作呢?
如果,你想,那你是不是很像知道,我们应该怎么让其弹出来一张选择列表,又应该怎么代码实现后边的操作呢?
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"));
执行之 会弹出一个对话框 效果为:
[代码]
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(); } }
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>
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")); }
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 Intent.ACTION_GET_CONTENT妙用
- android之Itent.ACTION_PICK Intent.ACTION_GET_CONTENT妙用
- android之Itent.ACTION_PICK Intent.ACTION_GET_CONTENT妙用
- android之Itent.ACTION_PICK Intent.ACTION_GET_CONTENT妙用
- android之Itent.ACTION_PICK Intent.ACTION_GET_CONTENT妙用
- android之Itent.ACTION_PICK Intent.ACTION_GET_CONTENT妙用
- Android 巧用Itent.ACTION_PICK和Intent.ACTION_GET_CONTENT
- android Itent学习之 ACTION_PICK&ACTION_GET_CONTENT
- Android 巧用Itent.ACTION_PICK和Intent.ACTION_GET_CONTENT(一)
- Android 巧用Itent.ACTION_PICK和Intent.ACTION_GET_CONTENT(二)
- Android 巧用Itent.ACTION_PICK和Intent.ACTION_GET_CONTENT(三)
- Android 巧用Itent.ACTION_PICK和Intent.ACTION_GET_CONTENT(三)
- Android获取本地图片之ACTION_GET_CONTENT与ACTION_PICK区别
- ACTION_PICK ACTION_GET_Content
- Android——ACTION_GET_CONTENT和ACTION_PICK
- Intent.ACTION_GET_CONTENT和Intent.ACTION_PICK的使用区别
- ACTION_PICK与ACTION_GET_CONTENT
- 配置魔药(medic.pas/c/cpp)
- Qt Plugin简单示例
- WinForm 窗体间传递数据的一些方法[转]
- MO_GLOBAL - EBS R12 中 Multi Org 设计的深入研究
- I Believe I Can Fly
- android之Itent.ACTION_PICK Intent.ACTION_GET_CONTENT妙用
- 理解值与引用[学习]
- JAVA数据流的概述
- C# 浅拷贝与深拷贝区别
- Linux环境编程--linux中的perror、exit、_exit、wait 和 waitpid
- JSP+Servlet登录页面制作
- 看懂SQL Server 查询计划[转]
- SQL Server 中 RAISERROR 的用法
- 2011-07-27 —— 杂记 —— SetEvent 和 PulseEvent 的区别