当前位置:  编程技术>移动开发
本页文章导读:
    ▪BaseExpandableListAdapter的施用        BaseExpandableListAdapter的使用 资源来自于网络----------------------->   图片见附件   项目需要展示一个通讯簿,通讯簿中的手机号码是分组的,要求勾选组时,自动勾选组下的手机号码,实现.........
    ▪ 施用广播机制来完成版本更新模块(详解)        运用广播机制来完成版本更新模块(详解) 先看下主配置文件:<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.xiaoma.www" android:versi.........
    ▪ 照相机的简单调用Demo       相机的简单调用Demo 主类:package com.mzh.www; import java.io.ByteArrayOutputStream; import java.io.File; import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.net.Uri; import android.........

[1]BaseExpandableListAdapter的施用
    来源: 互联网  发布时间: 2014-02-18
BaseExpandableListAdapter的使用

资源来自于网络----------------------->

 

图片见附件

 

项目需要展示一个通讯簿,通讯簿中的手机号码是分组的,要求勾选组时,自动勾选组下的手机号码,实现效果如下:


我们这个实例主要讲的就是当点击一个分组的时候,分组里的所有人就默认的全部选中,有了这个功能我们在群发的时候就会给我们省去不少麻烦,这样我们就可以不用一个一个的选中了。那么我们就来看看这个效果是怎么样实现吗。

下面是实现步骤。

1、新建类PhoneListItem,用于表示分组中的每一个手机号码。

Java代码:

2011-4-7 15:07:02 上传
下载附件 (36.65 KB)

 

2011-4-7 15:07:02 上传
下载附件 (36.65 KB)

 

  • package eoe.ydtf.android;

  • public class PhoneListItem {

  • public String phone,name;

  • public boolean checked;

  • public PhoneListItem(String _name,String _phone,boolean _checked){

  • name=_name;

  • phone=_phone;

  • checked=_checked;

  • }

  • }
  • 复制代码


    2、新建布局文件phone_list_item.xml,用于定义分组下面的每一条手机记录的页面布局。

    Java代码:

  • <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

  • android:layout_width="fill_parent"
  • android:layout_height="wrap_content"
  • android:orientation="horizontal"
  • android:minHeight="40px"
  • android:layout_gravity="center_vertical">

  • <CheckBox
  • android:id="@+id/phone_check"
  • android:focusable="false"
  • android:layout_width="wrap_content"
  • android:layout_height="wrap_content"
  • android:layout_marginLeft="35px"
  • android:checked="false"/>

  • <TextView
  • android:id="@+id/phone_name"
  • android:layout_width="80dip"
  • android:layout_height="wrap_content"
  • android:layout_gravity="center_vertical" />

  • <TextView
  • android:id="@+id/phone_number"
  • android:layout_width="wrap_content"
  • android:layout_height="wrap_content"
  • android:textColor="?android:attr/textColorPrimary"
  • android:paddingLeft="10px"
  • android:layout_gravity="center_vertical" />

  • </LinearLayout>
  • 复制代码

    这个主要就是我们在这里布局,你想要什么样的效果都可以,这里就是上面效果图的布局

    3、新建类PhoneGroup,用于表示一个分组。代码当中主要就是在一个分组里的内容,我们在用一个boolean值,这个就是说当你选中的时候是真,当你没有选中的时候是假,所以这个很主要,有了boolean值才能知道你是否选中。

    Java代码:

  • import java.util.List;

  • public class PhoneGroup {

  • public String title;

  • private boolean checked;

  • public List<PhoneListItem> children;

  • public PhoneGroup(String title,boolean checked,List<PhoneListItem> children){

  • this.title=title;

  • setChecked(checked);

  • this.children=children;

  • }

  • public boolean getChecked(){

  • return checked;

  • }

  • public void setChecked(boolean b){

  • checked=b;

  • if(children!=null&&children.size()>0){//若children不为空,循环设置children的checked

  • for(PhoneListItem each : children){

  • each.checked=checked;

  • }

  • }

  • }

  • }
  • 复制代码


    4、新建Activity布局文件phone_browser.xml,用于定义通讯簿的展现页面。

    Java代码:

  • <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

  • android:layout_width="fill_parent"
  • android:layout_height="fill_parent"
  • android:orientation="vertical">

  • <ExpandableListView
  • android:id="@+id/phonelist"
  • android:layout_width="fill_parent"
  • android:layout_height="0dip"
  • android:layout_weight="1" />

  • </LinearLayout>
  • 复制代码


    5、新建类QxtPhoneSelect,用于呈现通讯簿的Activity页面。

    Java代码:

  • package eoe.ydtf;

  • import java.util.ArrayList;
  • import java.util.List;
  • import com.ydtf.android.PhoneGroupAdapter.ExpandableListHolder;
  • import android.app.Activity;
  • import android.os.Bundle;
  • import android.view.View;

  • import android.widget.ExpandableListView;

  • public class QxtSelectPhone extends Activity implements

  • ExpandableListView.OnGroupClickListener,ExpandableListView.OnChildClickListener{

  • private List<PhoneGroup> groups;

  • private PhoneGroupAdapter exlist_adapter = null;

  • private ExpandableListView exlist;

  • public void onCreate(Bundle savedInstanceState) {

  • super.onCreate(savedInstanceState);

  • //加载layout

  • setContentView(R.layout.phone_browser);

  • //取得listview

  • exlist = (ExpandableListView) findViewById(R.id.phonelist);

  • //调用init方法,这个方法主要是,初始化一些数据

  • init();

  • //构建expandablelistview的适配器

  • exlist_adapter = new PhoneGroupAdapter(this, groups);

  • exlist.setOnChildClickListener(this);

  • exlist.setAdapter(exlist_adapter); //绑定视图-适配器

  • }

  • private void init() {

  • groups = new ArrayList<PhoneGroup>();


  • //构建List用作group1的子项

  • List<PhoneListItem> group1_children = new ArrayList<PhoneListItem>();

  • //往List中添加内容

  • PhoneListItem item = new PhoneListItem("和文明","1308763994", false);

  • group1_children.add(item);

  • item = new PhoneListItem("黄文明","1308763994", false);

  • group1_children.add(item);

  • item = new PhoneListItem("王文明","1308763994", false);

  • group1_children.add(item);

  • //拼装成 PhoneGroup

  • PhoneGroup phonegroup1=new PhoneGroup("group1",false,group1_children);

  • //------把前面的代码复制一遍,再添加一个组group2

  • //构建List用作group2的子项

  • List<PhoneListItem> group2_children = new ArrayList<PhoneListItem>();

  • //往List中添加内容

  • item = new PhoneListItem("张文明","1589065423", false);

  • group2_children.add(item);

  • item = new PhoneListItem("李文明","1589065423", false);

  • group2_children.add(item);

  • item = new PhoneListItem("赵文明","1589065423", false);

  • group2_children.add(item);

  • //拼装成 PhoneGroup

  • PhoneGroup phonegroup2=new PhoneGroup("group2",false,group2_children);

  • //添加进groups数组

  • groups.add(phonegroup1);

  • groups.add(phonegroup2);

  • }

  • //当分组行背点击时,让分组呈现“选中/取消选中”状态。

  • @Override
  • public boolean onChildClick(ExpandableListView parent, View v,

  • int groupPosition, int childPosition, long id) {

  • PhoneGroupAdapter.ExpandableListHolder holder=(ExpandableListHolder) v.getTag();

  • holder.chkChecked.setChecked(!holder.chkChecked.isChecked());

  • groups.get(groupPosition).children.get(childPosition).checked=

  • !groups.get(groupPosition).children.get(childPosition).checked;

  • return false;

  • }

  • @Override
  • public boolean onGroupClick(ExpandableListView parent, View v,

  • int groupPosition, long id) {

  • // groups.get(groupPosition).setChecked(!groups.get(groupPosition).getChecked());

  • // exlist_adapter.notifyDataSetChanged();

  • return false;

  • }

  • }
  • 复制代码


    6、新建类PhoneGroupAdapter,实现BaseExpandableListAdapter。

    Java代码:

  • package eoe.ydtf.android;


  • import java.util.List;
  • import android.content.Context;
  • import android.util.Log;
  • import android.view.LayoutInflater;
  • import android.view.View;
  • import android.view.View.OnClickListener;
  • import android.view.ViewGroup;
  • import android.widget.BaseExpandableListAdapter;
  • import android.widget.Button;
  • import android.widget.CheckBox;
  • import android.widget.CompoundButton;
  • import android.widget.CompoundButton.OnCheckedChangeListener;
  • import android.widget.TextView;


  • public class PhoneGroupAdapter extends BaseExpandableListAdapter {
  • class ExpandableListHolder { //定义一个内部类,用于保存listitem的3个子视图引用,2个textview和1个checkbox
  • TextView tvName;
  • TextView tvPhone;
  • CheckBox chkChecked;
  • }
  • private Context context; //父activity
  • private LayoutInflater mChildInflater; //用于加载listitem的布局xml
  • private LayoutInflater mGroupInflater; //用于加载group的布局xml
  • private List<PhoneGroup> groups; //所有group
  • //构造方法:参数c - activity,参数group - 所有group
  • public PhoneGroupAdapter(Context c,List<PhoneGroup> groups){
  • context=c;
  • mChildInflater = (LayoutInflater) context
  • .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  • mGroupInflater = (LayoutInflater) context
  • .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  • this.groups = groups;
  • }
  • @Override
  • public Object getChild(int arg0, int arg1) {//根据组索引和item索引,取得listitem // TODO Auto-generated method stub
  • return groups.get(arg0).children.get(arg1);
  • }
  • @Override
  • public long getChildId(int arg0, int arg1) {//返回item索引
  • return arg1;
  • }
  • @Override
  • public int getChildrenCount(int groupPosition) {//根据组索引返回分组的子item数
  • return groups.get(groupPosition).children.size();
  • }
  • @Override
  • public Object getGroup(int groupPosition) {//根据组索引返回组
  • return groups.get(groupPosition);
  • }
  • @Override
  • public int getGroupCount() {//返回分组数
  • return groups.size();
  • }
  • @Override
  • public long getGroupId(int groupPosition) {//返回分组索引
  • return groupPosition;
  • }
  • @Override
  • public View getGroupView(int position, boolean isExpanded,
  • View view, ViewGroup parent) {//根据组索引渲染"组视图"
  • ExpandableListHolder holder = null; //清空临时变量holder
  • if (view == null) { //判断view(即view是否已构建好)是否为空

  • //若组视图为空,构建组视图。注意flate的使用,R.layout.browser_expandable_list_item代表了
  • //已加载到内存的browser_expandable_list_item.xml文件
  • view = mGroupInflater.inflate(
  • R.layout.phone_list_item, null);
  • //下面主要是取得组的各子视图,设置子视图的属性。用tag来保存各子视图的引用
  • holder = new ExpandableListHolder();
  • //从view中取得textView
  • holder.tvName = (TextView) view.findViewById(R.id.phone_name);
  • //从view中取得textview
  • holder.tvPhone = (TextView) view.findViewById(R.id.phone_number);
  • //从view中取得checkbox
  • holder.chkChecked = (CheckBox) view
  • .findViewById(R.id.phone_check);
  • // holder.chkChecked.setEnabled(false);//禁用checkbox
  • //把checkbox、textview的引用保存到组视图的tag属性中
  • view.setTag(holder);
  • } else { //若view不为空,直接从view的tag属性中获得各子视图的引用
  • holder = (ExpandableListHolder) view.getTag();
  • }
  • //对应于组索引的组数据(模型)
  • PhoneGroup info = this.groups.get(position);
  • if (info != null) {
  • //根据模型值设置textview的文本
  • holder.tvName.setText(info.title);
  • //根据模型值设置checkbox的checked属性
  • holder.chkChecked.setChecked(info.getChecked());
  • holder.chkChecked.setTag(info);
  • holder.chkChecked.setOnClickListener(new OnClickListener(){
  • @Override
  • public void onClick(View v) {
  • PhoneGroup group=(PhoneGroup)v.getTag();
  • group.setChecked(!group.getChecked());
  • notifyDataSetChanged();
  • }
  • });
  • }
  • // TODO Auto-generated method stub
  • return view;
  • }
  • //行渲染方法
  • @Override
  • public View getChildView(int groupPosition, int childPosition,
  • boolean isLastChild, View convertView, ViewGroup parent) {
  • ExpandableListHolder holder = null; //清空临时变量
  • if (convertView == null) { //若行未初始化
  • //通过flater初始化行视图
  • convertView = mChildInflater.inflate(
  • R.layout.phone_list_item, null);
  • //并将行视图的3个子视图引用放到tag中
  • holder = new ExpandableListHolder();
  • holder.tvName = (TextView) convertView
  • .findViewById(R.id.phone_name);

  • holder.tvPhone = (TextView) convertView.findViewById(R.id.phone_number);
  • holder.chkChecked = (CheckBox) convertView
  • .findViewById(R.id.phone_check);
  • // holder.chkChecked.setEnabled(false);
  • convertView.setTag(holder);
  • } else { //若行已初始化,直接从tag属性获得子视图的引用
  • holder = (ExpandableListHolder) convertView.getTag();
  • }
  • //获得行数据(模型)
  • PhoneListItem info = this.groups.get(groupPosition)
  • .children.get(childPosition);

  • if (info != null) {
  • //根据模型数据,设置行视图的控件值
  • holder.tvName.setText(info.name);
  • holder.tvPhone.setText(info.phone);
  • holder.chkChecked.setChecked(info.checked);
  • holder.chkChecked.setTag(info);
  • holder.chkChecked.setOnClickListener(new OnClickListener(){
  • @Override
  • public void onClick(View v) {
  • CheckBox check=(CheckBox)v;
  • PhoneListItem item=(PhoneListItem)v.getTag();
  • item.checked=!item.checked;
  • // check.setChecked(!check.isChecked());
  • }

  • });
  • }
  • return convertView;
  • }
  • @Override
  • public boolean hasStableIds() {//行是否具有唯一id
  • return false;
  • }
  • @Override
  • public boolean isChildSelectable(int groupPosition, int childPosition) {//行是否可选
  • return true;
  • }
  • }

  •     
    [2] 施用广播机制来完成版本更新模块(详解)
        来源: 互联网  发布时间: 2014-02-18
    运用广播机制来完成版本更新模块(详解)
    先看下主配置文件:
    <?xml version="1.0" encoding="utf-8"?> 
    <manifest xmlns:android="http://schemas.android.com/apk/res/android" 
        package="com.xiaoma.www" 
        android:versionCode="20111111" 
        android:versionName="1.0" > 
     
        <uses-sdk android:minSdkVersion="15" /> 
     
        <application 
            android:icon="@drawable/ic_launcher" 
            android:label="@string/app_name" > 
            <activity 
                android:name=".BroadCastUpdateVersionActivity" 
                android:label="@string/app_name" > 
                <intent-filter> 
                    <action android:name="android.intent.action.MAIN" /> 
                    <category android:name="android.intent.category.LAUNCHER" /> 
                </intent-filter> 
            </activity> 
             
            <!-- 下载服务注册 --> 
            <service android:name=".DownloadService"></service> 
             
            <!-- XML配置方式实现广播注册 --> 
            <!-- <receiver android:enabled=["true" | "false"] 
              android:exported=["true" | "false"] 
              android:icon="drawable resource" 
              android:label="string resource" 
              android:name="string" 
              android:permission="string" 
              android:process="string" > 
              . . .这些是receiver里面的属性,爽吧?嘿嘿,不懂的朋友们可跟下链接 : 
                   http://developer.android.com/guide/topics/manifest/receiver-element.html 
            </receiver> --> 
            <!--   
                <receiver android:name="此处是你写的单独的广播子类,必须是完整路径" > 
                    <intent-filter> 
                        <action android:name="com.xiaoma.comeon"/> 
                    </intent-filter>             
                </receiver> 
            --> 
        </application> 
     
    </manifest> 


    再看下主控制类:
    package com.xiaoma.www; 
     
    import android.app.Activity; 
    import android.app.AlertDialog; 
    import android.app.Dialog; 
    import android.content.BroadcastReceiver; 
    import android.content.Context; 
    import android.content.DialogInterface; 
    import android.content.Intent; 
    import android.content.IntentFilter; 
    import android.content.pm.PackageManager.NameNotFoundException; 
    import android.net.Uri; 
    import android.os.Bundle; 
    import android.view.View; 
    import android.view.View.OnClickListener; 
    import android.widget.Button; 
    import android.widget.Toast; 
     
    /**   
    * @Title: BroadCastUpdateVersionActivity.java 
    * @Package com.xiaoma.www 
    * @Description: 运用广播机制来完成版本更新模块 
    * @author MZH 
    */ 
    public class BroadCastUpdateVersionActivity extends Activity { 
         
        private Button checkBtn; 
        private BroadcastUpdateReceiver bur = null ;  
        private static final String BROADCAST_ACTION = "com.xiaoma.comeon"; 
         
        /** Called when the activity is first created. */ 
        @Override 
        public void onCreate(Bundle savedInstanceState) { 
            super.onCreate(savedInstanceState); 
            setContentView(R.layout.main); 
             
            /** 
             * 此处:注册广播的方式两种,小马顺带讲下 
             * 方式一:XML方式在Manifest配置中注册    请看全局配置文件注册receiver 
             * 方式二:纯代码注册如下,此处需要特别注意的是,如果使用代码注册广播的话,注册必须在发起 
             *      广播之前 ,否则广播无效或报错,还有就是尽量用内部类实现广播时采用这种注册方式 
             */ 
             
            //注册是否有新版本广播    
            registerBroadCast(); 
            init(); 
        } 
         
        /** 
         * 是否有新版本广播注册实现 
         */ 
        private void registerBroadCast(){ 
            //注册之前先得有个BroadCastReceiver的子类 
            bur = new BroadcastUpdateReceiver(); 
            IntentFilter filter = new IntentFilter(); 
            //下面的BROADCAST_ACTION与全局配置中的receiver标签子标签 
            //intentfilter的action的name值是一致的哦 
            filter.addAction(BROADCAST_ACTION); 
            registerReceiver(bur, filter); 
        } 
         
        /** 
         * 内部类实现广播 
        * @Title: BroadCastUpdateVersionActivity.java 
        * @Package com.xiaoma.www 
        * @Description: 接收是否下载完成广播事件 
        * @author MZH 
         */ 
        public class BroadcastUpdateReceiver extends BroadcastReceiver{ 
     
            @Override 
            public void onReceive(Context context, Intent intent) { 
                Bundle bundle = intent.getExtras(); 
                String status = bundle.getString("服务器返回给你的标志位"); 
                if (status.equals("如:success")){ 
                    String urlStr = "file:///"+ bundle.getString("Path"); 
                    //以下这个Intent是指当下载完成后,用新的安装VIEW来提示用户是否安装刚下载好的APK 
                    Intent i = new Intent(Intent.ACTION_VIEW); 
                    //解析设置服务器返回数据类型 
                    intent.setDataAndType(Uri.parse(urlStr),  
                            "application/vnd.android.package-archive"); 
                    startActivity(intent); 
                }else{ 
                    Toast.makeText(BroadCastUpdateVersionActivity.this, "下载更新版本失败", Toast.LENGTH_SHORT).show(); 
                } 
     
                BroadCastUpdateVersionActivity.this.finish();  
            } 
        } 
         
        /** 
         * 初始化信息实现 
         */ 
        private void init(){ 
             checkBtn = (Button)findViewById(R.id.checkVersion); 
             checkBtn.setOnClickListener(new OnClickListener() { 
                 
                @Override 
                public void onClick(View v) { 
                     
                    /** 
                     * 这里小马讲下在AndroidManifest.xml文件中版本号的一些限制,小马注释写详细些 
                     * 希望大家不要嫌啰嗦,我只希望帮忙大家更多,了解得更透彻 
                     * 首先:http://developer.android.com/guide/topics/manifest/manifest-element.html 
                     * 可双上面的链接中看到如下信息: 
                     *   <manifest xmlns:android="http://schemas.android.com/apk/res/android" 
                          package="string" 
                          android:sharedUserId="string" 
                          android:sharedUserLabel="string resource"  
                          android:versionCode="integer" 
                          android:versionName="string" 
                                                顺便了解下下面这句啦,哈哈,激动,是设置我们的应用默认安装的位置:自动、手机内存、SD卡 
                          android:installLocation=["auto" | "internalOnly" | "preferExternal"] > 
                          . . . 
                          </manifest> 
                     * 跟进版本versionCode与versionNames可查看限制: 
                     * 再次强调一下,看到英文不要怕,不懂了用工具查,提高编程提高英文水平 
                     * versionCode中此句最重要: 
                     *    The value must be set as an integer, such as "100" 
                     * versionName也只看一句: 
                     *    The version number shown to users. 
                     *  
                     */ 
                    try { 
                        //取得当前应用的包 
                        String packageName = getPackageName(); 
                        //根据包名取得当前应用的版本号 
                        int myVersion = getPackageManager().getPackageInfo(packageName, 0).versionCode; 
                         
                        /** 
                         * 取服务器上的新版本文件版本号与当前应用的版本比较,如果当前版本低于服务器时弹出更新提示如下, 
                         * 访问服务器这步小马跳过,因为今天这个试例讲解广播实现版本更新嘛,通常使用webservice访问服务器 
                         * 朋友们可以把访问服务器的代码直接写到这个地方进行版本比对哦 
                         */ 
                        showAlertDialog(); 
                    } catch (NameNotFoundException e) { 
                        e.printStackTrace(); 
                    } 
                } 
            }); 
        } 
         
      //版本更新弹出对话框 
        private void showAlertDialog(){ 
            Dialog dialog = new AlertDialog.Builder(this) 
              .setTitle("新版本提示") 
            .setMessage("发现新版本,是否需要升!") 
              .setPositiveButton("确定",new DialogInterface.OnClickListener() { 
                  public void onClick(DialogInterface dialog,int whichButton) { 
                          //下载服务控制 
                          downloadService(); 
              }}).setNegativeButton("取消", new DialogInterface.OnClickListener(){ 
                public void onClick(DialogInterface dialog, int which) { 
                     dialog.dismiss(); 
                } 
              }).create(); 
            dialog.show(); 
       } 
         
        private void downloadService(){ 
            String PathName = "从服务器取到的完整的路径如:http://www.google.cn/webhp?source=g_cn/XXX.apk"; 
            Intent intent = new Intent(); 
            intent.putExtra("URL", "访问服务器的URL"); 
            intent.putExtra("Path", PathName); 
            //记得在全局配置中注册DownloadService服务 
            intent.setClass(this, DownloadService.class); 
            /** 
             * 因为是这种方式启动的service,小马就在此插张图,供朋友们理解在DownloadService.class里面 
             * 小马为什么把处理过程写在onStart()中而不是onBind()中。 
             */ 
            startService(intent); 
        } 
    } 



    下面来看下后台服务下载的代码:
    package com.xiaoma.www; 
     
    import java.io.File; 
    import java.io.InputStream; 
     
    import com.xiaoma.utils.FileUtils; 
     
    import android.app.Service; 
    import android.content.Intent; 
    import android.os.Bundle; 
    import android.os.IBinder; 
     
     
    /**   
    * @Title: DownloadService.java 
    * @Package com.xiaoma.www 
    * @Description: 启用后台下载新版本控制类 
    * @author MZH 
    */ 
    public class DownloadService extends Service { 
     
        @Override 
        public IBinder onBind(Intent intent) { 
            return null; 
        } 
         
        @Override 
        public void onStart(Intent intent, int startId) { 
            if(intent != null){ 
                String urlStr = intent.getStringExtra("URL"); 
                String fullPathName = intent.getStringExtra("Path"); 
                DownloadThread downloadThread = new DownloadThread(urlStr, fullPathName); 
                Thread thread = new Thread(downloadThread); 
                thread.start(); 
            } 
            super.onStart(intent, startId); 
        } 
     
        class DownloadThread implements Runnable{ 
            private String mUrl = null; 
            private String mPathName = null; 
             
            public DownloadThread(String aUrl, String PathName){ 
                mUrl = aUrl; 
                mPathName = PathName; 
            } 
             
            @Override 
            public void run() { 
                int result = downFile(mUrl, mPathName); 
                Intent intent = new Intent("com.xiaoma.comeon"); 
                Bundle bundle = new Bundle(); 
                if( -1 == result){    
                    bundle.putString("Status", "error"); 
                }else { 
                    bundle.putString("Status", "success"); 
                    bundle.putString("Path", mPathName); 
                } 
                intent.putExtras(bundle); 
                sendBroadcast(intent); 
            } 
        } 
         
        public int downFile(String urlStr, String fullPathName){ 
            InputStream inputStream = null; 
            try{ 
                File file = new File(fullPathName); 
                if (file.exists()){ 
                    return 1; 
                }else{ 
                    FileUtils fu = new FileUtils(); 
                    inputStream = fu.getStreamFromUrl(/blog_article/urlStr/index.html); 
                    File f = fu.writeInput(fullPathName, inputStream); 
                    if (f == null){ 
                        return -1; 
                    } 
                } 
            }catch(Exception e){ 
                e.printStackTrace(); 
                return -1; 
            }finally{ 
                try{ 
                    inputStream.close(); 
                } catch (Exception e) { 
                    e.printStackTrace(); 
                } 
            } 
            return 0; 
        } 
     
    } 


    以下是后台下载时用到的文件工具类代码:
    package com.xiaoma.utils; 
     
    import java.io.File; 
    import java.io.FileOutputStream; 
    import java.io.IOException; 
    import java.io.InputStream; 
    import java.io.OutputStream; 
    import java.net.HttpURLConnection; 
    import java.net.MalformedURLException; 
    import java.net.URL; 
     
    /** 
     * @Title: DeleteFile.java 
     * @Description: 文件、文件夹操作类 
     * @author MZH 
     */ 
    public class FileUtils{ 
        private URL url = null ; 
        public FileUtils(){ 
        } 
         
        public File writeInput(String PathName, InputStream input){ 
            File file = null; 
            OutputStream output = null; 
            try{ 
                file = new File( PathName ); 
                if (file.exists()) 
                    return file; 
                     
                int length = 0; 
                output = new FileOutputStream(file); 
                byte buffer[] = new byte[1024]; 
                while((length = input.read(buffer)) > 0){ 
                    output.write(buffer, 0, length); 
                } 
                 
            }catch(Exception e){ 
                e.printStackTrace(); 
            }finally{ 
                try{ 
                    output.close(); 
                }catch(Exception e){ 
                    e.printStackTrace(); 
                } 
            } 
             
            return file; 
        } 
         
        public InputStream getStreamFromUrl(/blog_article/String urlStr/index.html) 
                throws MalformedURLException, IOException { 
            url = new URL(urlStr); 
            HttpURLConnection urlConn = (HttpURLConnection) url.openConnection(); 
            InputStream inputStream = urlConn.getInputStream(); 
            return inputStream; 
        } 
    } 

        
    [3] 照相机的简单调用Demo
        来源: 互联网  发布时间: 2014-02-18
    相机的简单调用Demo
    主类:
    package com.mzh.www;
    
    import java.io.ByteArrayOutputStream;
    import java.io.File;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.graphics.Bitmap;
    import android.net.Uri;
    import android.os.Bundle;
    import android.os.Environment;
    import android.provider.MediaStore;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.Toast;
    
    /**  
    * @Title: CameraTestActivity.java
    * @Package com.lvguo.www
    * @Description: 相机测试
    * @author MZH
    * @version V2.2   老样子,写个自己喜欢的版本号,方便日后维护
    */
    public class CameraTestActivity extends Activity {
    	
    	private Button btn ;
    	private static final String IMAGE_UNSPECIFIED = "image/*";
    	String kkk = null;
    	
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            btn = (Button)findViewById(R.id.button1);
            btn.setOnClickListener(new OnClickListener() {
    			
    			@Override
    			public void onClick(View v) {
    				//第一次看见这个类,不认识,直接Android Developer里面Search   
    				//小x英文不是很好,时刻在电脑上挂着词典,也希望朋友们能准备一款自己喜爱的词典
    				Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    				//参数可直接在官方文档里面查到,所以朋友们练下手吧,自己查下EXTRA_OUTPUT
    				intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri
    						.fromFile(new File(Environment
    								.getExternalStorageDirectory(),
    								"mzh.jpg")));//temp为保存照片的文件名
    				
    				//此处讲几句,类似这些方法,小x建议直接在浏览器下查,因为文档查不到就说没查到,
    				//有网情况下,会自动定位到Activity | Android Developers直接点就OK了
    				
    				startActivityForResult(intent, 1); //这是个好东西哦
    				Toast.makeText(getApplicationContext(), "点击拍照", Toast.LENGTH_LONG).show();
    			}
    		});
        }
        
        
        /**
         * 用来处理startActivityForResult返回的数据,查此方法用老办法 :直接在浏览器下查
         */
    	@Override
    	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    		if (requestCode == 9) {  //此处的数字自己定,可随便  
    			Bundle extras = data.getExtras();
    			if (extras != null) {
    				Bitmap photo = extras.getParcelable("data");
    				ByteArrayOutputStream stream = new ByteArrayOutputStream();
    				//英语不好的朋友,拿工具查compress是什么意思就明白了,小x英语一般,所以此处多说一句,工具
    				photo.compress(Bitmap.CompressFormat.JPEG, 60, stream);
    				byte[] b = stream.toByteArray();
    				
    				
    				//Base64Coder是一个剪切压缩辅助类,里面的东西我也看不懂,不过没事,直接用,因为要用的方法不多,情况具体定
    				kkk = new String(Base64Coder.encodeLines(b));
    			}
    
    		}
    		if(requestCode == 1){
    			File picture = new File(Environment.getExternalStorageDirectory()
    					+ "/mzh.jpg");
    			cutPic(Uri.fromFile(picture));
    		}
    		super.onActivityResult(requestCode, resultCode, data);
    	}
    	
    	public void cutPic(Uri uri) {
    		//下面这一句第一眼看到我不懂,没事,直接Android Developer里面Search  Intent 
    		
    		//熟悉下面Summary吗?用工具查什么意思,再找下它是什么地方的,是官方文档最右侧顶端 的东西,里面有个Ctros,点进去
    		//Summary: Nested Classes | Constants | Inherited Constants | Fields 
    		//| Ctors | Methods | Inherited Methods | [Expand All]
    		//会发现有个构造方法:Intent(String action)。。。。再点进去,贴官方代码:
    		/**
    		 * public Intent (String action)
    
    		Since: API Level 1
    			Create an intent with a given action. All other fields (data, type, class) are null. 
    			Note that the action must be in a namespace because Intents are used globally in 
    			the system -- for example the system VIEW action is android.intent.action.VIEW; 
    			an application's custom action would be something like com.google.app.myapp.CUSTOM_ACTION.
    		Parameters
    
     			<看到这句了吧?ACTION_VIEW,不知道的可以再Search>
    			action	The Intent action, such as ACTION_VIEW.
    			
    			 要再不明白的朋友可以直接在有网情况下直接把com.android.camera.action.CROP Search一下就OK了,
    			 小x查询文档的能力不强,所以在查询这块多说几句
    
    		 */
    		Intent intent = new Intent("com.android.camera.action.CROP");
    		intent.setDataAndType(uri, IMAGE_UNSPECIFIED);
    		intent.putExtra("crop", "true");
    		// aspectX aspectY 是宽高的比例
    		intent.putExtra("aspectX", 1);
    		intent.putExtra("aspectY", 1);
    		// outputX outputY 是裁剪图片宽高
    		intent.putExtra("outputX", 150);
    		intent.putExtra("outputY", 150);
    		intent.putExtra("return-data", true);
    		startActivityForResult(intent, 9);
    	}
        
        
        
        
    }


    Base64Coder.java
    package com.mzh.www;
    
    //Copyright 2003-2010 Christian d'Heureuse, Inventec Informatik AG, Zurich, Switzerland
    //www.source-code.biz, www.inventec.ch/chdh
    //
    //This module is multi-licensed and may be used under the terms
    //of any of the following licenses:
    //
    //EPL, Eclipse Public License, V1.0 or later, http://www.eclipse.org/legal
    //LGPL, GNU Lesser General Public License, V2.1 or later, http://www.gnu.org/licenses/lgpl.html
    //GPL, GNU General Public License, V2 or later, http://www.gnu.org/licenses/gpl.html
    //AL, Apache License, V2.0 or later, http://www.apache.org/licenses
    //BSD, BSD License, http://www.opensource.org/licenses/bsd-license.php
    //
    //Please contact the author if you need another license.
    //This module is provided "as is", without warranties of any kind.
    
    
    /**
    * A Base64 encoder/decoder.
    *
    * <p>
    * This class is used to encode and decode data in Base64 format as described in RFC 1521.
    *
    * <p>
    * Project home page: <a href="http://www.source-code.biz/base64coder/java/">www.source-code.biz/base64coder/java</a><br>
    * Author: Christian d'Heureuse, Inventec Informatik AG, Zurich, Switzerland<br>
    * Multi-licensed: EPL / LGPL / GPL / AL / BSD.
    */
    public class Base64Coder {
    
    //The line separator string of the operating system.
    private static final String systemLineSeparator = System.getProperty("line.separator");
    
    //Mapping table from 6-bit nibbles to Base64 characters.
    private static char[]    map1 = new char[64];
    static {
       int i=0;
       for (char c='A'; c<='Z'; c++) map1[i++] = c;
       for (char c='a'; c<='z'; c++) map1[i++] = c;
       for (char c='0'; c<='9'; c++) map1[i++] = c;
       map1[i++] = '+'; map1[i++] = '/'; }
    
    //Mapping table from Base64 characters to 6-bit nibbles.
    private static byte[]    map2 = new byte[128];
    static {
       for (int i=0; i<map2.length; i++) map2[i] = -1;
       for (int i=0; i<64; i++) map2[map1[i]] = (byte)i; }
    
    /**
    * Encodes a string into Base64 format.
    * No blanks or line breaks are inserted.
    * @param s  A String to be encoded.
    * @return   A String containing the Base64 encoded data.
    */
    public static String encodeString (String s) {
    return new String(encode(s.getBytes())); }
    
    /**
    * Encodes a byte array into Base 64 format and breaks the output into lines of 76 characters.
    * This method is compatible with <code>sun.misc.BASE64Encoder.encodeBuffer(byte[])</code>.
    * @param in  An array containing the data bytes to be encoded.
    * @return    A String containing the Base64 encoded data, broken into lines.
    */
    public static String encodeLines (byte[] in) {
    return encodeLines(in, 0, in.length, 76, systemLineSeparator); }
    
    /**
    * Encodes a byte array into Base 64 format and breaks the output into lines.
    * @param in            An array containing the data bytes to be encoded.
    * @param iOff          Offset of the first byte in <code>in</code> to be processed.
    * @param iLen          Number of bytes to be processed in <code>in</code>, starting at <code>iOff</code>.
    * @param lineLen       Line length for the output data. Should be a multiple of 4.
    * @param lineSeparator The line separator to be used to separate the output lines.
    * @return              A String containing the Base64 encoded data, broken into lines.
    */
    public static String encodeLines (byte[] in, int iOff, int iLen, int lineLen, String lineSeparator) {
    int blockLen = (lineLen*3) / 4;
    if (blockLen <= 0) throw new IllegalArgumentException();
    int lines = (iLen+blockLen-1) / blockLen;
    int bufLen = ((iLen+2)/3)*4 + lines*lineSeparator.length();
    StringBuilder buf = new StringBuilder(bufLen);
    int ip = 0;
    while (ip < iLen) {
       int l = Math.min(iLen-ip, blockLen);
       buf.append (encode(in, iOff+ip, l));
       buf.append (lineSeparator);
       ip += l; }
    return buf.toString(); }
    
    /**
    * Encodes a byte array into Base64 format.
    * No blanks or line breaks are inserted in the output.
    * @param in  An array containing the data bytes to be encoded.
    * @return    A character array containing the Base64 encoded data.
    */
    public static char[] encode (byte[] in) {
    return encode(in, 0, in.length); }
    
    /**
    * Encodes a byte array into Base64 format.
    * No blanks or line breaks are inserted in the output.
    * @param in    An array containing the data bytes to be encoded.
    * @param iLen  Number of bytes to process in <code>in</code>.
    * @return      A character array containing the Base64 encoded data.
    */
    public static char[] encode (byte[] in, int iLen) {
    return encode(in, 0, iLen); }
    
    /**
    * Encodes a byte array into Base64 format.
    * No blanks or line breaks are inserted in the output.
    * @param in    An array containing the data bytes to be encoded.
    * @param iOff  Offset of the first byte in <code>in</code> to be processed.
    * @param iLen  Number of bytes to process in <code>in</code>, starting at <code>iOff</code>.
    * @return      A character array containing the Base64 encoded data.
    */
    public static char[] encode (byte[] in, int iOff, int iLen) {
    int oDataLen = (iLen*4+2)/3;       // output length without padding
    int oLen = ((iLen+2)/3)*4;         // output length including padding
    char[] out = new char[oLen];
    int ip = iOff;
    int iEnd = iOff + iLen;
    int op = 0;
    while (ip < iEnd) {
       int i0 = in[ip++] & 0xff;
       int i1 = ip < iEnd ? in[ip++] & 0xff : 0;
       int i2 = ip < iEnd ? in[ip++] & 0xff : 0;
       int o0 = i0 >>> 2;
       int o1 = ((i0 &   3) << 4) | (i1 >>> 4);
       int o2 = ((i1 & 0xf) << 2) | (i2 >>> 6);
       int o3 = i2 & 0x3F;
       out[op++] = map1[o0];
       out[op++] = map1[o1];
       out[op] = op < oDataLen ? map1[o2] : '='; op++;
       out[op] = op < oDataLen ? map1[o3] : '='; op++; }
    return out; }
    
    /**
    * Decodes a string from Base64 format.
    * No blanks or line breaks are allowed within the Base64 encoded input data.
    * @param s  A Base64 String to be decoded.
    * @return   A String containing the decoded data.
    * @throws   IllegalArgumentException If the input is not valid Base64 encoded data.
    */
    public static String decodeString (String s) {
    return new String(decode(s)); }
    
    /**
    * Decodes a byte array from Base64 format and ignores line separators, tabs and blanks.
    * CR, LF, Tab and Space characters are ignored in the input data.
    * This method is compatible with <code>sun.misc.BASE64Decoder.decodeBuffer(String)</code>.
    * @param s  A Base64 String to be decoded.
    * @return   An array containing the decoded data bytes.
    * @throws   IllegalArgumentException If the input is not valid Base64 encoded data.
    */
    public static byte[] decodeLines (String s) {
    char[] buf = new char[s.length()+3];
    int p = 0;
    for (int ip = 0; ip < s.length(); ip++) {
       char c = s.charAt(ip);
       if (c != ' ' && c != '\r' && c != '\n' && c != '\t')
          buf[p++] = c; }
       while ((p % 4) != 0)
    	   buf[p++] = '0';
    	
    return decode(buf, 0, p); }
    
    /**
    * Decodes a byte array from Base64 format.
    * No blanks or line breaks are allowed within the Base64 encoded input data.
    * @param s  A Base64 String to be decoded.
    * @return   An array containing the decoded data bytes.
    * @throws   IllegalArgumentException If the input is not valid Base64 encoded data.
    */
    public static byte[] decode (String s) {
    return decode(s.toCharArray()); }
    
    /**
    * Decodes a byte array from Base64 format.
    * No blanks or line breaks are allowed within the Base64 encoded input data.
    * @param in  A character array containing the Base64 encoded data.
    * @return    An array containing the decoded data bytes.
    * @throws    IllegalArgumentException If the input is not valid Base64 encoded data.
    */
    public static byte[] decode (char[] in) {
    return decode(in, 0, in.length); }
    
    /**
    * Decodes a byte array from Base64 format.
    * No blanks or line breaks are allowed within the Base64 encoded input data.
    * @param in    A character array containing the Base64 encoded data.
    * @param iOff  Offset of the first character in <code>in</code> to be processed.
    * @param iLen  Number of characters to process in <code>in</code>, starting at <code>iOff</code>.
    * @return      An array containing the decoded data bytes.
    * @throws      IllegalArgumentException If the input is not valid Base64 encoded data.
    */
    public static byte[] decode (char[] in, int iOff, int iLen) {
    if (iLen%4 != 0) throw new IllegalArgumentException ("Length of Base64 encoded input string is not a multiple of 4.");
    while (iLen > 0 && in[iOff+iLen-1] == '=') iLen--;
    int oLen = (iLen*3) / 4;
    byte[] out = new byte[oLen];
    int ip = iOff;
    int iEnd = iOff + iLen;
    int op = 0;
    while (ip < iEnd) {
       int i0 = in[ip++];
       int i1 = in[ip++];
       int i2 = ip < iEnd ? in[ip++] : 'A';
       int i3 = ip < iEnd ? in[ip++] : 'A';
       if (i0 > 127 || i1 > 127 || i2 > 127 || i3 > 127)
          throw new IllegalArgumentException ("Illegal character in Base64 encoded data.");
       int b0 = map2[i0];
       int b1 = map2[i1];
       int b2 = map2[i2];
       int b3 = map2[i3];
       if (b0 < 0 || b1 < 0 || b2 < 0 || b3 < 0)
          throw new IllegalArgumentException ("Illegal character in Base64 encoded data.");
       int o0 = ( b0       <<2) | (b1>>>4);
       int o1 = ((b1 & 0xf)<<4) | (b2>>>2);
       int o2 = ((b2 &   3)<<6) |  b3;
       out[op++] = (byte)o0;
       if (op<oLen) out[op++] = (byte)o1;
       if (op<oLen) out[op++] = (byte)o2; }
    return out; }
    
    //Dummy constructor.
    private Base64Coder() {}
    
    } // end class Base64Coder


    配置文件:
      <?xml version="1.0" encoding="utf-8" ?> 
    - <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.mzh.www" android:versionCode="1" android:versionName="1.0">
      <uses-sdk android:minSdkVersion="8" /> 
    - <application android:icon="@drawable/icon" android:label="@string/app_name">
    - <activity android:name=".CameraTestActivity" android:label="@string/app_name">
    - <intent-filter>
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter>
      </activity>
      </application>
      <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
      <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /> 
    - <!--  相机使用权限 
      --> 
      <uses-permission android:name="android.permission.CAMERA" /> 
      <uses-feature android:name="android.hardware.camera" /> 
      <uses-feature android:name="android.hardware.camera.autofocus" /> 
      </manifest>

        
    最新技术文章:
    ▪Android开发之登录验证实例教程
    ▪Android开发之注册登录方法示例
    ▪Android获取手机SIM卡运营商信息的方法
    ▪Android实现将已发送的短信写入短信数据库的...
    ▪Android发送短信功能代码
    ▪Android根据电话号码获得联系人头像实例代码
    ▪Android中GPS定位的用法实例
    ▪Android实现退出时关闭所有Activity的方法
    ▪Android实现文件的分割和组装
    ▪Android录音应用实例教程
    ▪Android双击返回键退出程序的实现方法
    ▪Android实现侦听电池状态显示、电量及充电动...
    ▪Android获取当前已连接的wifi信号强度的方法
    ▪Android实现动态显示或隐藏密码输入框的内容
    ▪根据USER-AGENT判断手机类型并跳转到相应的app...
    ▪Android Touch事件分发过程详解
    ▪Android中实现为TextView添加多个可点击的文本
    ▪Android程序设计之AIDL实例详解
    ▪Android显式启动与隐式启动Activity的区别介绍
    ▪Android按钮单击事件的四种常用写法总结
    ▪Android消息处理机制Looper和Handler详解
    ▪Android实现Back功能代码片段总结
    ▪Android实用的代码片段 常用代码总结
    ▪Android实现弹出键盘的方法
    ▪Android中通过view方式获取当前Activity的屏幕截...
    ▪Android提高之自定义Menu(TabMenu)实现方法
    ▪Android提高之多方向抽屉实现方法
    ▪Android提高之MediaPlayer播放网络音频的实现方法...
    ▪Android提高之MediaPlayer播放网络视频的实现方法...
    ▪Android提高之手游转电视游戏的模拟操控
     


    站内导航:


    特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

    ©2012-2021,,E-mail:www_#163.com(请将#改为@)

    浙ICP备11055608号-3