import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.Locale;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.ActionBarActivity;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity implements OnClickListener {
private ImageView imageView;
private Button buttonTake, buttonChoose;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.img_show);
buttonTake = (Button) findViewById(R.id.btn_takePohto);
buttonChoose = (Button) findViewById(R.id.btn_choosePohto);
buttonTake.setOnClickListener(this);
buttonChoose.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btn_takePohto:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 1);
break;
case R.id.btn_choosePohto:
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, 2);
break;
default:
break;
}
}
private Uri photoUri;
private String path;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 1:
String sdStatus = Environment.getExternalStorageState();
if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 检测sd是否可用
Log.i("TestFile",
"SD card is not avaiable/writeable right now.");
return;
}
new DateFormat();
String name = DateFormat.format("yyyyMMdd_hhmmss",
Calendar.getInstance(Locale.CHINA))
+ ".jpg";
Toast.makeText(this, name, Toast.LENGTH_LONG).show();
Bundle bundle = data.getExtras();
Bitmap bitmap = (Bitmap) bundle.get("data");// 获取相机返回的数据,并转换为Bitmap图片格式
FileOutputStream b = null;
File file = new File("/sdcard/Image/");
file.mkdirs();// 创建文件夹
String fileName = "/sdcard/Image/" + name;
try {
b = new FileOutputStream(fileName);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, b);// 把数据写入文件
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
b.flush();
b.close();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
imageView.setImageBitmap(bitmap);// 将图片显示在ImageView里
} catch (Exception e) {
Log.e("error", e.getMessage());
}
break;
case 2:
if (resultCode == Activity.RESULT_OK) {
if (data != null) {
photoUri = data.getData();
}
if (photoUri != null) {
String[] pojo = { MediaStore.Images.Media.DATA };
System.err.println(pojo[0] + "----pojo[0]----");
Cursor cursor = managedQuery(photoUri, pojo, null, null,null);
if (cursor != null) {
int columeIndex = cursor.getColumnIndexOrThrow(pojo[0]);
cursor.moveToFirst();
path = cursor.getString(columeIndex);
cursor.close();
}
Log.i("test", "imagePath = " + path);
if (path != null
&& (path.endsWith(".png") || path.endsWith(".PNG")
|| path.endsWith(".jpg") || path.endsWith(".JPG"))) {
imageView.setImageBitmap(BitmapFactory.decodeFile(path));
}
}
}
break;
default:
break;
}
/* */
}
}
布局文件:
<LinearLayout 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"
android:orientation="vertical"
tools:context="com.example.photodemo.MainActivity" >
<ImageView
android:id="@+id/img_show"
android:layout_width="match_parent"
android:layout_height="180dp"
android:scaleType="centerInside"
android:padding="15dp"
android:src="@drawable/ic_launcher" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal"
>
<Button
android:id="@+id/btn_takePohto"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="拍照"
/>
<Button
android:id="@+id/btn_choosePohto"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="选择"
/>
</LinearLayout>
</LinearLayout>
权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<uses-feature android:name = "android.hardware.camera" />
<uses-feature android:name = "android.hardware.camera.autofocus" />