android 使用系统照相程序照相并存储、显示在界面上

大部分业务可以通过调用系统的相机程序来拍照。

界面如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
        >

    <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="前往拍照"
            android:onClick="goToPicture"
            />

    <ImageView
            android:id="@+id/imageView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            />
</LinearLayout>

java:

package com.example.usingSystemCamera;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.widget.ImageView;

import java.io.File;

public class MyActivity extends Activity {

    private File file;//相片文件
    private ImageView imageView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        imageView = (ImageView) findViewById(R.id.imageView);
    }

    public void goToPicture(View view) {
        Intent intent = new Intent();
        intent.setAction("android.media.action.IMAGE_CAPTURE");
        intent.addCategory("android.intent.category.DEFAULT");
        file = new File(Environment.getExternalStorageDirectory(), "myJpg.jpg");
        Uri uri = Uri.fromFile(file);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
        startActivityForResult(intent, 0);
    }

    /**
     * 当保存了文件后,设置到界面上
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Bitmap bitmap = BitmapFactory.decodeFile(file.getPath(), null);
        imageView.setImageBitmap(bitmap);
    }
}

 

posted @ 2014-04-13 15:05  无忧之路  阅读(270)  评论(0编辑  收藏  举报
无忧之路