安卓调用系统相机,通过路径返回清晰图片

废话不多,功能简单,直接上代码:

首先是布局代码:


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.glx.myxiangji.MainActivity">

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

    <TextView
        android:id="@+id/xiangji"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="相机1" />

    <TextView
        android:id="@+id/xiangji2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_marginTop="30dp"
        android:text="相机2" />

RelativeLayout>

布局很简单,不用说什么了

public class MainActivity extends Activity implements View.OnClickListener {

    private TextView xiangji;
    private TextView xiangji2;
    private int XX = 123;
    private int XX2 = 1223;
    private String mFilePath;
    private ImageView img;

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

        //获取SD卡路径,设置文件存储路径
        mFilePath = Environment.getExternalStorageDirectory().getPath()+"/photo.png";

        xiangji = (TextView) findViewById(R.id.xiangji);
        xiangji2 = (TextView) findViewById(R.id.xiangji2);
        xiangji2.setOnClickListener(this);
        xiangji.setOnClickListener(this);
        img = (ImageView) findViewById(R.id.img);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.xiangji) {
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(intent, XX);
        }else {
            if (v.getId() == R.id.xiangji2) {
                Uri uri = Uri.fromFile(new File(mFilePath));
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT,uri);
                startActivityForResult(intent, XX2);
            }
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == XX) {
                Bundle bundle = data.getExtras();
                Bitmap bitmap = (Bitmap) bundle.get("data");
                img.setImageBitmap(bitmap);
            }else {
                if (requestCode == XX2) {
                    Bitmap bitmap = BitmapFactory.decodeFile(mFilePath);
                    img.setImageBitmap(bitmap);
                }
            }
        }
    }
}

全部代码就这么多,主要是通过
if (v.getId() == R.id.xiangji2) {
Uri uri = Uri.fromFile(new File(mFilePath));
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT,uri);
startActivityForResult(intent, XX2);
}

设置相机照片保存路径,然后在onActivityResult方法里为ImageView设置图片。

你可能感兴趣的:(技术代码)