日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

android之Itent.ACTION_PICK Intent.ACTION_GET_CONTENT妙用

發布時間:2025/3/19 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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屬性來判斷具體調用哪個程序的。
  • Intent?intent?=?new?Intent(Intent.ACTION_GET_CONTENT); ??
  • intent.setType("audio/*"); ??
  • startActivity(Intent.createChooser(intent,?"Select?music"));??
  • [java]?view plaincopy
  • <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??播放目標

    ?

    Java代碼
  • 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(); ??
  • ????} ??
  • }??
  • [java]?view plaincopy
  • <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

    ?

    Java代碼
  • <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>??
  • [java]?view plaincopy
  • <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

    ?

    Java代碼
  • 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")); ??
  • ????}??
  • [java]?view plaincopy
  • <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妙用的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。