布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.takephoto.MainActivity" >
<Button
android:id="@+id/btn_openCamera"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="click"
android:text="openCamera" />
<ImageView
android:id="@+id/iv_photo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/ic_launcher" />
</LinearLayout>
全部代码:
package com.example.takephoto;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
public class MainActivity extends ActionBarActivity {
private String photo_name;
private ImageView mImageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mImageView = (ImageView)findViewById(R.id.iv_photo);
}
public void click(View view) {
switch (view.getId()) {
case R.id.btn_openCamera:
String currentTime = getCurrentTime();
photo_name = currentTime + ".jpg";
if (isSDAvaiable()) {
Log.i("click", "sd可用");
Log.i("click", "开启相机");
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(
MediaStore.EXTRA_OUTPUT,
Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/"
+ photo_name)));
startActivityForResult(intent, 1);
} else {
Log.i("click", "sd不可用");
}
break;
default:
break;
}
}
@Override
protected void onActivityResult(int arg0, int arg1, Intent arg2) {
if (arg0 == 1) {
Bitmap bitmap = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory()
+ "/" + photo_name);
Bitmap resizeBitmap = resizeBitmap(bitmap, 640);
mImageView.setImageBitmap(resizeBitmap);
}
super.onActivityResult(arg0, arg1, arg2);
}
private boolean isSDAvaiable() {
String externalStorageState = Environment.getExternalStorageState();
if (externalStorageState.equals(Environment.MEDIA_MOUNTED)) {
return true;
} else {
return false;
}
}
public static Bitmap resizeBitmap(Bitmap bitmap, int newWidth) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
float temp = ((float)height) / ((float)width);
int newHeight = (int)((newWidth) * temp);
float scaleWidth = ((float)newWidth) / width;
float scaleHeight = ((float)newHeight) / height;
Matrix matrix = new Matrix();
// resize the bit map
matrix.postScale(scaleWidth, scaleHeight);
// matrix.postRotate(45);
Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
bitmap.recycle();
return resizedBitmap;
}
private String getCurrentTime() {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss ");
Date curDate = new Date(System.currentTimeMillis());
String timeString = simpleDateFormat.format(curDate);
return timeString;
}
}
demo:
网盘资源地址