Android calls the system camera method and saves photos

MainActivity:


public class MainActivity extends Activity implements View.OnClickListener {


    public static final int REQ_1 = 1;
    public static final int REQ_2 = 2;

    //获取拍摄之后在本地的路径
    private String mFilePath;
    @Bind(R.id.show_photo_thumail)
    ImageView showPhotoThumail;
    @Bind(R.id.launch_system_camera4imageview)
    Button launchSystemCamera4imageview;
    @Bind(R.id.launch_system_camera4SD)
    Button launchSystemCamera4SD;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        mFilePath = Environment.getExternalStorageDirectory().getPath();
        //我们指定的路径
        mFilePath = mFilePath + "/" + "temp.png";
        launchSystemCamera4SD.setOnClickListener(this);
        launchSystemCamera4imageview.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //保存在imageview中
            case R.id.launch_system_camera4imageview:
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(intent, REQ_1);
                break;
            //保存在SD卡或者本机中
            case R.id.launch_system_camera4SD:
                Intent intent2 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                //把Uri指向了我们创建的文件对象
                Uri photo_uri = Uri.fromFile(new File(mFilePath));
                //通过MediaStore.EXTRA_OUTPUT我们可以对拍完照片之后的照片存储路径进行更改
                intent2.putExtra(MediaStore.EXTRA_OUTPUT, photo_uri);
                startActivityForResult(intent2, REQ_2);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == REQ_1) {
                Bundle bundle = data.getExtras();
                Bitmap bitmap = (Bitmap) bundle.get("data");
                showPhotoThumail.setImageBitmap(bitmap);
            } else {
                if (requestCode == REQ_2) {
                    FileInputStream fis = null;
                    try {
                        fis = new FileInputStream(mFilePath);
                        Bitmap bitmap = BitmapFactory.decodeStream(fis);
                        showPhotoThumail.setImageBitmap(bitmap);
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } finally {
                        try {
                            fis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }

                }
            }
        }
    }
}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent"
   >
<LinearLayout
    android:layout_width="match_parent"
    android:orientation="horizontal"
    android:layout_height="wrap_content">
    <Button
        android:layout_width="0dp"
        android:layout_weight="1"
        android:id="@+id/launch_system_camera4imageview"
        android:layout_height="wrap_content"
        android:text="保存在imageView" />
    <Button
        android:layout_width="0dp"
        android:layout_weight="1"
        android:id="@+id/launch_system_camera4SD"
        android:layout_height="wrap_content"
        android:text="保存在本地" />
</LinearLayout>

    <ImageView
        android:layout_width="match_parent"
        android:id="@+id/show_photo_thumail"
        android:layout_height="match_parent" />
</LinearLayout>
Permissions and filters in AndroidManifest.xml:
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<intent-filter>
                <action android:name="android.media.action.IMAGE_CAPTURE" />
                <category android:name="android.intent.category.DEFAULT" />
</intent-filter>


Inspiration video address: http://www.imooc.com/learn/543

Source code download address: http://download.csdn.net/detail/u010111008/9309597

Guess you like

Origin blog.csdn.net/u010111008/article/details/50086639