网友通过本文主要向大家介绍了andriod编程软件,andriod studio 教程,andriodmarket,安卓 andriod,andriod7.0等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
andriod打开摄像头和打开相册,andriod摄像头相册
package com.example.yanlei.picture; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import java.io.File; import java.io.IOException; import android.app.Activity; import android.content.Intent; import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Matrix; import android.media.ExifInterface; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private static final String tag = "MainActivity"; private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100; private static final int PICK_IMAGE_ACTIVITY_REQUEST_CODE = 200; private ImageView imageView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageView = (ImageView) this.findViewById(R.id.image_view); Button button = (Button) this.findViewById(R.id.open_camera); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { takePicture(); } }); Button pickImageBtn = (Button) this.findViewById(R.id.pick_image); pickImageBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { openAlbum(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } private static String picFileFullName; //拍照 public void takePicture(){ String state = Environment.getExternalStorageState(); if (state.equals(Environment.MEDIA_MOUNTED)) { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); File outDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); if (!outDir.exists()) { outDir.mkdirs(); } File outFile = new File(outDir, System.currentTimeMillis() + ".jpg"); picFileFullName = outFile.getAbsolutePath(); intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(outFile)); intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1); startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE); } else{ Log.e(tag, "请确认已经插入SD卡"); } } //打开本地相册 public void openAlbum(){ Intent intent = new Intent(); intent.setType("image/*"); intent.setAction(Intent.ACTION_GET_CONTENT); this.startActivityForResult(intent, PICK_IMAGE_ACTIVITY_REQUEST_CODE); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) { if (resultCode == RESULT_OK) { Log.e(tag, "获取图片成功,path="+picFileFullName); toast("获取图片成功,path="+picFileFullName); setImageView(picFileFullName); } else if (resultCode == RESULT_CANCELED) { // 用户取消了图像捕获 } else { // 图像捕获失败,提示用户 Log.e(tag, "拍照失败"); } } else if (requestCode == PICK_IMAGE_ACTIVITY_REQUEST_CODE) { if (resultCode == RESULT_OK) { Uri uri = data.getData(); if(uri != null){ String realPath = getRealPathFromURI(uri); Log.e(tag, "获取图片成功,path="+realPath); toast("获取图片成功,path="+realPath); setImageView(realPath); }else{ Log.e(tag, "从相册获取图片失败"); } } } } private void setImageView(String realPath){ Bitmap bmp = BitmapFactory.decodeFile(realPath); int degree = readPictureDegree(realPath); if(degree <= 0){ imageView.setImageBitmap(bmp); }else{ Log.e(tag, "rotate:"+degree); //创建操作图片是用的matrix对象 Matrix matrix=new Matrix(); //旋转图片动作