2013年11月6日 星期三

[google api] MediaStore (呼叫intent)


******* 敘述
The Media provider contains meta data for all available media on both internal and external storage devices.
******

參考網址:




  1 、布局文件: 主要有两个:带listview的browse_app_list.xml文件 ;listview的项browse_app_item.xml
browse_app_list.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical" android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent">>  
  5.     <ListView android:id="@+id/listviewApp" android:layout_width="fill_parent"  
  6.         android:layout_height="fill_parent" ></ListView>  
  7. </LinearLayout>  

browse_app_item.xmlbrowse_app_item.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent" android:layout_height="50dip">  
  4.   
  5.     <ImageView android:id="@+id/imgApp" android:layout_width="wrap_content"  
  6.         android:layout_height="fill_parent" ></ImageView>  
  7.     <RelativeLayout android:layout_width="fill_parent"  android:layout_marginLeft="10dip"  
  8.         android:layout_height="40dip">  
  9.         <TextView android:id="@+id/tvLabel" android:layout_width="wrap_content"  
  10.             android:layout_height="wrap_content" android:text="AppLable : "></TextView>  
  11.         <TextView android:id="@+id/tvAppLabel" android:layout_width="wrap_content"  
  12.             android:layout_toRightOf="@id/tvLabel" android:layout_height="wrap_content"  
  13.             android:layout_marginLeft="3dip" android:text="Label" android:textColor="#FFD700"></TextView>  
  14.         <TextView android:id="@+id/tvName" android:layout_width="wrap_content"  
  15.             android:layout_height="wrap_content" android:layout_below="@id/tvLabel"   
  16.             android:text="包名:"></TextView>  
  17.         <TextView android:id="@+id/tvPkgName" android:layout_width="wrap_content"  
  18.             android:layout_height="wrap_content" android:layout_below="@id/tvAppLabel"  
  19.             android:layout_alignLeft="@id/tvAppLabel" android:textColor="#FFD700"></TextView>  
  20.     </RelativeLayout>  
  21. </LinearLayout>  
       
2 、AppInfo.java : 保存应用程序信息的Model类
  1. /Model类 ,用来存储应用程序信息  
  2. public class AppInfo {  
  3.     
  4.     private String appLabel;    //应用程序标签  
  5.     private Drawable appIcon ;  //应用程序图像  
  6.     private Intent intent ;     //启动应用程序的Intent ,一般是Action为Main和Category为Lancher的Activity  
  7.     private String pkgName ;    //应用程序所对应的包名  
  8.       
  9.     public AppInfo(){}  
  10.       
  11.     public String getAppLabel() {  
  12.         return appLabel;  
  13.     }  
  14.     public void setAppLabel(String appName) {  
  15.         this.appLabel = appName;  
  16.     }  
  17.     public Drawable getAppIcon() {  
  18.         return appIcon;  
  19.     }  
  20.     public void setAppIcon(Drawable appIcon) {  
  21.         this.appIcon = appIcon;  
  22.     }  
  23.     public Intent getIntent() {  
  24.         return intent;  
  25.     }  
  26.     public void setIntent(Intent intent) {  
  27.         this.intent = intent;  
  28.     }  
  29.     public String getPkgName(){  
  30.         return pkgName ;  
  31.     }  
  32.     public void setPkgName(String pkgName){  
  33.         this.pkgName=pkgName ;  
  34.     }  
  35. }  
 3、 BrowseApplicationInfoAdapter.java : 自定义适配器类,为ListView提供视图
  1. //自定义适配器类,提供给listView的自定义view  
  2. public class BrowseApplicationInfoAdapter extends BaseAdapter {  
  3.       
  4.     private List<AppInfo> mlistAppInfo = null;  
  5.       
  6.     LayoutInflater infater = null;  
  7.       
  8.     public BrowseApplicationInfoAdapter(Context context,  List<AppInfo> apps) {  
  9.         infater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  10.         mlistAppInfo = apps ;  
  11.     }  
  12.     @Override  
  13.     public int getCount() {  
  14.         // TODO Auto-generated method stub  
  15.         System.out.println("size" + mlistAppInfo.size());  
  16.         return mlistAppInfo.size();  
  17.     }  
  18.     @Override  
  19.     public Object getItem(int position) {  
  20.         // TODO Auto-generated method stub  
  21.         return mlistAppInfo.get(position);  
  22.     }  
  23.     @Override  
  24.     public long getItemId(int position) {  
  25.         // TODO Auto-generated method stub  
  26.         return 0;  
  27.     }  
  28.     @Override  
  29.     public View getView(int position, View convertview, ViewGroup arg2) {  
  30.         System.out.println("getView at " + position);  
  31.         View view = null;  
  32.         ViewHolder holder = null;  
  33.         if (convertview == null || convertview.getTag() == null) {  
  34.             view = infater.inflate(R.layout.browse_app_item, null);  
  35.             holder = new ViewHolder(view);  
  36.             view.setTag(holder);  
  37.         }   
  38.         else{  
  39.             view = convertview ;  
  40.             holder = (ViewHolder) convertview.getTag() ;  
  41.         }  
  42.         AppInfo appInfo = (AppInfo) getItem(position);  
  43.         holder.appIcon.setImageDrawable(appInfo.getAppIcon());  
  44.         holder.tvAppLabel.setText(appInfo.getAppLabel());  
  45.         holder.tvPkgName.setText(appInfo.getPkgName());  
  46.         return view;  
  47.     }  
  48.   
  49.     class ViewHolder {  
  50.         ImageView appIcon;  
  51.         TextView tvAppLabel;  
  52.         TextView tvPkgName;  
  53.   
  54.         public ViewHolder(View view) {  
  55.             this.appIcon = (ImageView) view.findViewById(R.id.imgApp);  
  56.             this.tvAppLabel = (TextView) view.findViewById(R.id.tvAppLabel);  
  57.             this.tvPkgName = (TextView) view.findViewById(R.id.tvPkgName);  
  58.         }  
  59.     }  
  60. }  

4 、MainActivity.java 主工程逻辑 
          请仔细体会queryIntentActivities()方法,并且注意到排序,它很重要。
  1. <span style="font-size:13px;">public class MainActivity extends Activity implements OnItemClickListener {  
  2.   
  3.     private ListView listview = null;  
  4.   
  5.     private List<AppInfo> mlistAppInfo = null;  
  6.   
  7.     @Override  
  8.     public void onCreate(Bundle savedInstanceState) {  
  9.         super.onCreate(savedInstanceState);  
  10.         setContentView(R.layout.browse_app_list);  
  11.   
  12.         listview = (ListView) findViewById(R.id.listviewApp);  
  13.         mlistAppInfo = new ArrayList<AppInfo>();  
  14.         queryAppInfo(); // 查询所有应用程序信息  
  15.         BrowseApplicationInfoAdapter browseAppAdapter = new BrowseApplicationInfoAdapter(  
  16.                 this, mlistAppInfo);  
  17.         listview.setAdapter(browseAppAdapter);  
  18.         listview.setOnItemClickListener(this);  
  19.     }  
  20.     // 点击跳转至该应用程序  
  21.     public void onItemClick(AdapterView<?> arg0, View view, int position,  
  22.             long arg3) {  
  23.         // TODO Auto-generated method stub  
  24.         Intent intent = mlistAppInfo.get(position).getIntent();  
  25.         startActivity(intent);  
  26.     }  
  27.     // 获得所有启动Activity的信息,类似于Launch界面  
  28.     public void queryAppInfo() {  
  29.         PackageManager pm = this.getPackageManager(); // 获得PackageManager对象  
  30.         Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);  
  31.         mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);  
  32.         // 通过查询,获得所有ResolveInfo对象.  
  33.         List<ResolveInfo> resolveInfos = pm  
  34.                 .queryIntentActivities(mainIntent, PackageManager.MATCH_DEFAULT_ONLY);  
  35.         // 调用系统排序 , 根据name排序  
  36.         // 该排序很重要,否则只能显示系统应用,而不能列出第三方应用程序  
  37.         Collections.sort(resolveInfos,new ResolveInfo.DisplayNameComparator(pm));  
  38.         if (mlistAppInfo != null) {  
  39.             mlistAppInfo.clear();  
  40.             for (ResolveInfo reInfo : resolveInfos) {  
  41.                 String activityName = reInfo.activityInfo.name; // 获得该应用程序的启动Activity的name  
  42.                 String pkgName = reInfo.activityInfo.packageName; // 获得应用程序的包名  
  43.                 String appLabel = (String) reInfo.loadLabel(pm); // 获得应用程序的Label  
  44.                 Drawable icon = reInfo.loadIcon(pm); // 获得应用程序图标  
  45.                 // 为应用程序的启动Activity 准备Intent  
  46.                 Intent launchIntent = new Intent();  
  47.                 launchIntent.setComponent(new ComponentName(pkgName,  
  48.                         activityName));  
  49.                 // 创建一个AppInfo对象,并赋值  
  50.                 AppInfo appInfo = new AppInfo();  
  51.                 appInfo.setAppLabel(appLabel);  
  52.                 appInfo.setPkgName(pkgName);  
  53.                 appInfo.setAppIcon(icon);  
  54.                 appInfo.setIntent(launchIntent);  
  55.                 mlistAppInfo.add(appInfo); // 添加至列表中  
  56.                 System.out.println(appLabel + " activityName---" + activityName  
  57.                         + " pkgName---" + pkgName);  
  58.             }  
  59.         }  
  60.     }  
  61. }

之後找到了 列出特定的intent的方法


main.xml
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
<Button
   android:id="@+id/captureimage"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="使用其他服務幫忙拍照 "
   />
<ImageView
   android:id="@+id/imagecaptured"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   />
</LinearLayout>

DemoCameraActivity.java
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package jim.demo.camera;
 
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
 
public class DemoCameraActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
         
        //調用其他服務拍照按鈕
        Button buttonCamera = (Button)findViewById(R.id.captureimage);
         
         
        
        buttonCamera.setOnClickListener(new Button.OnClickListener(){
   public void onClick(View v) {
    //使用Intent調用其他服務幫忙拍照
     Intent intent_camera = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
     startActivityForResult(intent_camera, 0);
   }
          
        });
         
         
    }
     
    @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      //拍照後顯示圖片
      ImageView iv = (ImageView)findViewById(R.id.imagecaptured);
   if (resultCode == RESULT_OK)
     {
    //取出拍照後回傳資料
    Bundle extras = data.getExtras();
    //將資料轉換為圖像格式
          Bitmap bmp = (Bitmap) extras.get("data");
          //載入ImageView
          iv.setImageBitmap(bmp);
     }
    
     //覆蓋原來的Activity
     super.onActivityResult(requestCode, resultCode, data);
  }
}

把這兩個 sample code 合併起來就完成了!!

重點在這行 (可以列出有關於相機的apk)
Intent intent_camera = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);