相机相册的使用

(权限)

    <uses-permission android:name="android.permission.INTERNET" />
    <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" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

(布局)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="zhangyanran20181023.bwie.com.login.PaiActivity">
    <ImageView
        android:src="@mipmap/ic_launcher"
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <Button
        android:text="拍照"
        android:id="@+id/paizhao"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <Button
        android:text="相册"
        android:id="@+id/xc"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

(activity代码)

public class PaiActivity extends AppCompatActivity {
    Button paizhao, xc;
    ImageView img;
    //定义一个私有的变量来拍照回调
    private String path;
    //定义静态变量来记录相册
    private static final int IMAGE = 1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pai);
        img = findViewById(R.id.img);
        paizhao =  findViewById(R.id.paizhao);
        xc =  findViewById(R.id.xc);
        paizhao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                path= Environment.getExternalStorageDirectory().getPath()+"/"+System.currentTimeMillis()+".jpg";
                Uri uri=Uri.fromFile(new File(path));//处理存放位置
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//创建拍照的意图
                intent .putExtra(MediaStore.EXTRA_OUTPUT, uri);//指定处理方的位置为系统的沼泽的存放位置
                startActivityForResult(intent, 1);
            }
        });
        //调用相册的相片回传
        xc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(intent,IMAGE);
            }
        });
    }
    // 进行的相册里面来获得code来判断
    // 调用相册
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //       调用相机拍照  把相机拍的照传回 该图片的控件
        Uri uri = Uri.fromFile(new File(path));
        img.setImageURI(uri);
        //获取图片路径
        if (requestCode == IMAGE && resultCode == Activity.RESULT_OK && data != null) {
            Uri selectedImage = data.getData();
            String[] filePathColumns = {MediaStore.Images.Media.DATA};
            Cursor c = getContentResolver().query(selectedImage, filePathColumns, null, null, null);
            c.moveToFirst();
            int columnIndex = c.getColumnIndex(filePathColumns[0]);
            String imagePath = c.getString(columnIndex);
            showImage(imagePath);
            c.close();
        }
    }
    private void showImage(String imaePath) {
        Bitmap bm = BitmapFactory.decodeFile(imaePath);
        img.setImageBitmap(bm);
    }
}
<ImageView
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:layout_marginBottom="10dp"
        android:scaleX="3"
        android:scaleY="8"
        android:src="@drawable/y" />

猜你喜欢

转载自blog.csdn.net/H_BuilDer/article/details/83379876