LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

android照相及照片上传实现代码

admin
2013年2月25日 13:26 本文热度 4366
本文章来介绍了关于手机开发之android来实现android照相及照片功能,有需要学习的同学可以参考一下。
 代码如下
package com.android.cist.camera.view; 
import java.io.ByteArrayOutputStream; 
import java.io.InputStream; 
import java.util.HashMap; 
import java.util.Map; 
import android.app.Activity; 
import android.app.AlertDialog; 
import android.content.ContentResolver; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.net.Uri; 
import android.os.Bundle; 
import android.os.Handler; 
import android.os.Message; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.ImageView; 
import android.widget.LinearLayout; 
import android.widget.Toast; 
import com.android.cist.R; 
import com.android.cist.network.HttpUtil; 
import com.android.cist.network.form.FormFile; 
import com.android.cist.network.form.HttpFormUtil; 
import com.android.cist.util.ApplicationUtil; 
import com.android.cist.util.ImageUtil; 
public class CameraActivity extends Activity implements OnClickListener{ 
    private ImageView preview; 
    private EditText carNo; 
    private EditText addres; 
    private EditText type; 
    private Button back; 
    private Button submit; 
    private Button capture; 
    private Button reCapture; 
    private OnClickListener imgViewListener; 
    private Bitmap myBitmap; 
    private byte[] mContent; 
    private Handler handler; 
    String returnString; 
    private CalendarView happenDate; 
    private static final int REQUEST_CAMERA = 1; 
    private static final int REQUEST_CALENDAR = 2; 
    @ Override 
    public void onCreate ( Bundle savedInstanceState ) 
    { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.camera); 
        setUpViews(); 
        setUpListeners(); 
        ApplicationUtil.getInstance().addActivity(this); 
    } 
    @ Override 
    protected void onActivityResult ( int requestCode , int resultCode , Intent data ) 
    { 
        super.onActivityResult(requestCode, resultCode, data); 
        ContentResolver resolver = getContentResolver(); 
        /**
         * 因为两种方式都用到了startActivityForResult方法,
         * 这个方法执行完后都会执行onActivityResult方法, 所以为了区别到底选择了那个方式获取图片要进行判断,
         * 这里的requestCode跟startActivityForResult里面第二个参数对应
         */ 
        if (requestCode == 0) 
        { 
            try 
            { 
                // 获得图片的uri 
                Uri originalUri = data.getData(); 
                // 将图片内容解析成字节数组 
                mContent = readStream(resolver.openInputStream(Uri.parse(originalUri.toString()))); 
                // 将字节数组转换为ImageView可调用的Bitmap对象 
                myBitmap = getPicFromBytes(mContent, null); 
                // //把得到的图片绑定在控件上显示 
                preview.setImageBitmap(myBitmap); 
            } catch ( Exception e ) 
            { 
                System.out.println(e.getMessage()); 
            } 
        } else if (requestCode == REQUEST_CAMERA){ 
            try 
            { 
                super.onActivityResult(requestCode, resultCode, data); 
                Bundle extras = data.getExtras(); 
                myBitmap = (Bitmap) extras.get("data"); 
                ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
                myBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); 
                mContent = baos.toByteArray(); 
            } catch ( Exception e ) 
            { 
                e.printStackTrace(); 
            } 
            // 把得到的图片绑定在控件上显示 
            preview.setImageBitmap(ImageUtil.toRoundCorner(myBitmap, 10));//把拍摄的照片转成圆角显示在预览控件上 
        }else if(requestCode==REQUEST_CALENDAR){ 
            if(resultCode == RESULT_OK){ 
                happenDate.setCalendar(data.getIntExtra("year", 1900), data.getIntExtra("month", 0), data.getIntExtra("day", 1)); 
            } 
        } 
    } 
    public static Bitmap getPicFromBytes ( byte[] bytes , BitmapFactory.Options opts ) 
    { 
        if (bytes != null) 
            if (opts != null) 
                return BitmapFactory.decodeByteArray(bytes, 0, bytes.length, opts); 
            else 
                return BitmapFactory.decodeByteArray(bytes, 0, bytes.length); 
        return null; 
    } 
    public static byte[] readStream ( InputStream inStream ) throws Exception 
    { 
        byte[] buffer = new byte[1024]; 
        int len = -1; 
        ByteArrayOutputStream outStream = new ByteArrayOutputStream(); 
        while ((len = inStream.read(buffer)) != -1) 
        { 
            outStream.write(buffer, 0, len); 
        } 
        byte[] data = outStream.toByteArray(); 
        outStream.close(); 
        inStream.close(); 
        return data; 
    } 
    @Override 
    public void onClick(View v) { 
        int id = v.getId(); 
        switch(id){ 
            case R.id.capture:{ 
                final CharSequence[] items = 
                { "相册", "拍照" }; 
                AlertDialog dlg = new AlertDialog.Builder(CameraActivity.this).setTitle("选择图片").setItems(items, 
                        new DialogInterface.OnClickListener() 
                        { 
                            public void onClick ( DialogInterface dialog , int item ) 
                            { 
                                // 这里item是根据选择的方式, 
                                // 在items数组里面定义了两种方式,拍照的下标为1所以就调用拍照方法 
                                if (item == 1){ 
                                    Intent getImageByCamera = new Intent("android.media.action.IMAGE_CAPTURE"); 
                                    startActivityForResult(getImageByCamera, REQUEST_CAMERA); 
                                } else{ 
                                    Intent getImage = new Intent(Intent.ACTION_GET_CONTENT); 
                                    getImage.addCategory(Intent.CATEGORY_OPENABLE); 
                                    getImage.setType("image/jpeg"); 
                                    startActivityForResult(getImage, 0); 
                                } 
                            } 
                        }).create(); 
                dlg.show(); 
            } 
            break; 
            /*case R.id.reCapture:{
                final CharSequence[] items =
                { "相册", "拍照" };
                AlertDialog dlg = new AlertDialog.Builder(CameraActivity.this).setTitle("选择图片").setItems(items,
                        new DialogInterface.OnClickListener()
                        {
                            public void onClick ( DialogInterface dialog , int item )
                            {
                                // 这里item是根据选择的方式,
                                // 在items数组里面定义了两种方式,拍照的下标为1所以就调用拍照方法
                                if (item == 1)
                                {
                                    Intent getImageByCamera = new Intent("android.media.action.IMAGE_CAPTURE");
                                    startActivityForResult(getImageByCamera, 1);
                                } else
                                {
                                    Intent getImage = new Intent(Intent.ACTION_GET_CONTENT);
                                    getImage.addCategory(Intent.CATEGORY_OPENABLE);
                                    getImage.setType("image/jpeg");
                                    startActivityForResult(getImage, 0);
                                }
                            }
                        }).create();
                dlg.show();
            }
            break;*/ 
            case R.id.submit:{ 
                new PostThread().start();//开启线程提交数据 
            } 
            break; 
            case R.id.back:{ 
                CameraActivity.this.finish();//返回,销毁当前Activity 
            } 
            break; 
            default: 
            break; 
        } 
    } 

该文章在 2013/2/25 13:26:08 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2024 ClickSun All Rights Reserved