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"));
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("audio/*");
startActivity(Intent.createChooser(intent, "Select music"));

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

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

[代码]

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. }
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

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>
<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

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. }
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);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android Studio中的action_get_content是一个活动(Activity)的操作,用于从其他应用程序或系统中获取内容。 当我们在Android应用程序中需要使用其他应用程序或系统提供的特定类型的数据时,例如图片、音频、视频等,我们就可以使用action_get_content操作。 通过调用该操作,我们可以打开Android设备上的文件浏览器或者其他应用程序,并选择需要的内容。一旦内容被选择,Android系统会返回一个表示所选内容的URI给我们的应用程序。 我们可以使用以下代码来执行此操作: ```java Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType("image/*"); // 指定获取图片类型的内容 startActivityForResult(intent, PICK_IMAGE_REQUEST); ``` 以上代码中,我们创建了一个新的Intent对象,并设置其操作为ACTION_GET_CONTENT。然后,我们使用setType()方法来指定获取图片类型的内容。最后,我们使用startActivityForResult()方法来启动该操作,并指定一个请求代码PICK_IMAGE_REQUEST。这样,一旦内容被选择,Android系统会将其返回给我们的应用程序,并调用onActivityResult()方法。 在onActivityResult()方法中,我们可以获取所选内容的URI,并进行进一步的处理,例如显示图片或将其上传到服务器。 总结起来,action_get_contentAndroid Studio中用于从其他应用程序或系统中获取内容的操作。它使我们的应用程序能够与其他应用程序或系统进行交互,并获取所需内容,以满足用户的需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值