package com.example.administrator.testapplication; import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Environment; import android.provider.MediaStore; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.KeyEvent; import android.view.View; import android.view.Window; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.ImageView; import java.io.File; import java.io.IOException; import java.math.BigDecimal; public class MainActivity extends Activity { private ImageView img; private static final String TEMP_PATH = Environment.getExternalStorageDirectory().getAbsolutePath() + "/tmp/temp_" + System.currentTimeMillis() + ".jpg"; private static final int REQ_CARMER = 0x002; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); isFolderExists(Environment.getExternalStorageDirectory().getAbsolutePath() + "/tmp/");//判断目录是否存在 checkFile(TEMP_PATH);//检查文件是否存在 img = (ImageView) findViewById(R.id.img); findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { takePhoto(); } }); } //检查文件是否存在 private void checkFile(String path) { File file = new File(path); if (!file.exists()) { try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } } //判断目录是否存在 boolean isFolderExists(String strFolder) { File file = new File(strFolder); if (!file.exists()) { if (file.mkdirs()) { return true; } else { return false; } } return true; } private void takePhoto() { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); //path为保存图片的路径,执行完拍照以后能保存到指定的路径下 File file = new File(TEMP_PATH); intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file)); intent.putExtra(MediaStore.Images.Media.ORIENTATION, 0); startActivityForResult(intent, REQ_CARMER); } //图片压缩 public Bitmap setImage(String path, Intent intent) { BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(path, options); // double value = new BigDecimal(options.outWidth).divide(new BigDecimal(options.outHeight), 4, BigDecimal.ROUND_HALF_UP).doubleValue(); // intent.putExtra(Constants.ISNULL, false); // intent.putExtra(Constants.IMGPATH, path); // intent.putExtra(Constants.ASPECT_RATIO, value == 0d ? "1.0000" : format.format(value)); options.inJustDecodeBounds = false; int be = Math.max(options.outWidth, options.outHeight) / 20; if (be % 10 != 0) be += 10; be = be / 10; if (be <= 0) be = 1; options.inSampleSize = be; return BitmapFactory.decodeFile(path, options); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == RESULT_OK) { switch (requestCode) { case REQ_CARMER: img.setImageBitmap(setImage(TEMP_PATH, new Intent())); break; } } super.onActivityResult(requestCode, resultCode, data); } }
不要忘了添加权限:
<uses-permission android:name="android.permission.CAMERA"></uses-permission> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>