1. 程式人生 > >安卓呼叫系統相機拍照並且顯示在ImageView上

安卓呼叫系統相機拍照並且顯示在ImageView上

並沒有什麼技術難點,只是在儲存到sdCard的時候有一點小細節需要注意,所以寫了這篇文章。程式碼很簡單,就不解釋什麼了,直接貼上原始碼。

public class TakePhotoActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_take_photo);

        ((Button) findViewById(R.id.takephoto)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

                startActivityForResult(intent, 1);
            }
        });
    }


    @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)){
                System.out.println(" ------------- sd card is not avaiable ---------------");
                return;
            }


            String name = "photo.jpg";

            Bundle bundle = data.getExtras();
            Bitmap bitmap = (Bitmap) bundle.get("data");

            File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/");
            file.mkdirs(); //建立資料夾

            String fileName = Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+name;

            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) findViewById(R.id.picture)).setImageBitmap(bitmap);

        }
    }
}
佈局檔案一個Button、一個ImageView
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_take_photo"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.zhuandian.eventbus.takephoto.TakePhotoActivity">


    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="take photo"
        android:id="@+id/takephoto"
        />
    <ImageView
        android:layout_below="@+id/takephoto"
        android:layout_width="400dp"
        android:layout_height="400dp"
        android:id="@+id/picture"
        />
</RelativeLayout>

由於呼叫系統相機需要給許可權,不要忘了在manifest檔案裡面給許可權宣告
<uses-permission android:name="android.permission.CAMERA" />

    <uses-feature android:name="android.hardware.camera" />

    <uses-feature android:name="android.hardware.camera.autofocus" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />


相關推薦

no