当前位置:编程学习 > JAVA >>

Java乔晓松-android中调用系统拍照功能并显示拍照的图片

android中调用系统拍照功能并显示拍照的图片

如果你是拍照完,利用onActivityResult获取data数据,把data数据转换成Bitmap数据,这样获取到的图片,是拍照的照片的缩略图

代码如下:

[html]
package com.example.myphotos; 
 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.text.SimpleDateFormat; 
import java.util.Date; 
import android.annotation.SuppressLint; 
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.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.ImageView; 
 
/** 
 * 2013-6-27 上午10:27:23 
 *  
 * @author 乔晓松 
 */ 
public class CameraActivity extends Activity { 
 
    private Button button; 
    private ImageView imageView; 
    private String fileName; 
 
    @SuppressLint({ "SimpleDateFormat", "SdCardPath" }) 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        this.setContentView(R.layout.activity_camera); 
        button = (Button) findViewById(R.id.btn_camera); 
        imageView = (ImageView) findViewById(R.id.imageView1); 
        File file = new File("/sdcard/myImage/"); 
        file.mkdirs();// 创建文件夹 
 
        button.setOnClickListener(new OnClickListener() { 
 
            @Override 
            public void onClick(View v) { 
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
                                startActivityForResult(intent, 1); 
            } 
        }); 
        // Intent intent = new Intent(); 
        // intent.setAction("android.intent.action.MAIN"); 
        // intent.addCategory("android.intent.category.LAUNCHER"); 
        // intent.setFlags(0x10200000); 
        // intent.setComponent(new ComponentName("com.android.camera", 
        // "com.android.camera.Camera")); 
        // startActivity(intent); 
    } 
 
    @SuppressLint({ "SdCardPath", "SimpleDateFormat" }) 
    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
        super.onActivityResult(requestCode, resultCode, data); 
        if (resultCode == Activity.RESULT_OK) { 
 
                String sdStatus = Environment.getExternalStorageState(); 
            if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 检测sd是否可用 
                Log.v("TestFile", 
                        "SD card is not avaiable/writeable right now."); 
                return; 
            } 
                            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH.mm.ss");String name = format.format(new Date());fileName = "/sdcard/myImage/" + name + ".jpg"; 
            Bundle bundle = data.getExtras(); 
            Bitmap bitmap = (Bitmap) bundle.get("data");// 获取相机返回的数据,并转换为Bitmap图片格式 
            FileOutputStream b = null; 
 
            try { 
                b = new FileOutputStream(fileName); 
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, b);// 把数据写入文件 
            } catch (FileNotFoundException e) { 
                e.printStackTrace(); 
            } finally { 
                try { 
                    b.flush(); 
                    b.close(); 
                } catch (IOException e) { 
                    e.printStackTrace(); 
                } 
            } 
            imageView.setImageBitmap(bitmap);// 将图片显示在ImageView里 
        } 
 
    } 

package com.example.myphotos;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputSt

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,