欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android 拍照和上传的实现代码

程序员文章站 2022-10-09 15:33:45
复制代码 代码如下:import java.io.bytearrayoutputstream;   import java.io.file; ...
复制代码 代码如下:

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.imageview;  
public class photoactivity extends activity {  
    public static final int none = 0;  
    public static final int photohraph = 1;// 拍照  
    public static final int photozoom = 2; // 缩放  
    public static final int photoresoult = 3;// 结果  
    public static final string image_unspecified = "image/*";  
    imageview imageview = null;  
    button button0 = null;  
    button button1 = null;  
    @override 
    public void oncreate(bundle savedinstancestate) {  
        super.oncreate(savedinstancestate);  
        setcontentview(r.layout.photo);  
        imageview = (imageview) findviewbyid(r.id.imageid);  
        button0 = (button) findviewbyid(r.id.btn_01);  
        button1 = (button) findviewbyid(r.id.btn_02);  
        button0.setonclicklistener(new onclicklistener() {  
            @override 
            public void onclick(view v) {  
                intent intent = new intent(intent.action_pick, null);  
                intent.setdataandtype(  
                        mediastore.images.media.external_content_uri,  
                        image_unspecified);  
                startactivityforresult(intent, photozoom);  
            }  
        });  
        button1.setonclicklistener(new onclicklistener() {  
            @override 
            public void onclick(view v) {  
                intent intent = new intent(mediastore.action_image_capture);  
                intent.putextra(mediastore.extra_output, uri.fromfile(new file(  
                        environment.getexternalstoragedirectory(), "temp.jpg")));  
                startactivityforresult(intent, photohraph);  
            }  
        });  
    }  
    @override 
    protected void onactivityresult(int requestcode, int resultcode, intent data) {  
        if (resultcode == none)  
            return;  
        // 拍照  
        if (requestcode == photohraph) {  
            // 设置文件保存路径这里放在跟目录下  
            file picture = new file(environment.getexternalstoragedirectory()  
                    + "/temp.jpg");  
            startphotozoom(uri.fromfile(picture));  
        }  
        if (data == null)  
            return;  
        // 读取相册缩放图片  
        if (requestcode == photozoom) {  
            startphotozoom(data.getdata());  
        }  
        // 处理结果  
        if (requestcode == photoresoult) {  
            bundle extras = data.getextras();  
            if (extras != null) {  
                bitmap photo = extras.getparcelable("data");  
                bytearrayoutputstream stream = new bytearrayoutputstream();  
                photo.compress(bitmap.compressformat.jpeg, 75, stream);// (0 -  
                                                                        // 100)压缩文件  
                imageview.setimagebitmap(photo);  
            }  
        }  
        super.onactivityresult(requestcode, resultcode, data);  
    }  
    public void startphotozoom(uri uri) {  
        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", 64);  
        intent.putextra("outputy", 64);  
        intent.putextra("return-data", true);  
        startactivityforresult(intent, photoresoult);  
    }  
}