这是一个完整的例子,复制就能跑
小米手机掉不出相册,其他手机能正常运行程序
布局
<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" >
<ImageView
android:id="@+id/im_picture"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/ic_launcher"
android:layout_gravity="center_horizontal"
/>
<Button
android:id="@+id/btn_take_photo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="拍照" />
<Button
android:id="@+id/btn_choose_from_album"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="从相册中选择" />
</LinearLayout>
//java代码:
package com.zhh.android;
import java.io.File;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.provider.MediaStore.Audio.Media;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
public static final int TAKE_PHOTO = 1;
public static final int CROP_PHOTO = 2;
private ImageView im_picture;
private Button btn_takePhoto;
private Button btn_choose_from_album;
private Uri imageUri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
im_picture = (ImageView) findViewById(R.id.im_picture);
btn_takePhoto = (Button) findViewById(R.id.btn_take_photo);
btn_choose_from_album=(Button) findViewById(R.id.btn_choose_from_album);
//调用系统相机拍照
btn_takePhoto.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 创建File对象,用于存储拍照后的照片
File outputImage = new File(Environment
.getExternalStorageDirectory(), "tempImage.jpg");
try {
if (outputImage.exists()) {
outputImage.delete();
}
outputImage.createNewFile();
} catch (Exception e) {
e.printStackTrace();
}
imageUri = Uri.fromFile(outputImage);
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, TAKE_PHOTO);// 启动相机程序
}
});
//从相册中拿到照片
btn_choose_from_album.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//创建File对象,用来存储选择的照片
//sd卡根目录
File outputImage=new File(Environment.getExternalStorageDirectory(),"output_image.jpg");
try {
if (outputImage.exists()) {
outputImage.delete();
}
outputImage.createNewFile();
} catch (Exception e) {
e.printStackTrace();
}
imageUri =Uri.fromFile(outputImage);
Intent intent=new Intent("android.intent.action.GET_CONTENT");
intent.setType("image/*");
intent.putExtra("crop", true);
intent.putExtra("scale", true);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, CROP_PHOTO);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case TAKE_PHOTO:
if (resultCode == RESULT_OK) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(imageUri, "image/*");
intent.putExtra("scale", true);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, CROP_PHOTO);// 启动裁剪程序
}
break;
case CROP_PHOTO:
try {
if (resultCode == RESULT_OK) {
Log.i("1","开始剪切");
Bitmap bitmap = BitmapFactory
.decodeStream(getContentResolver().openInputStream(
imageUri));
Log.i("1","剪贴完成");
im_picture.setImageBitmap(bitmap);// 将裁剪后的图片显示出来
Log.i("1", "把图片放到界面");
}
} catch (Exception e) {
e.printStackTrace();
}
break;
}
}
}// class
//主清单文件中开sd卡权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
我的相关博客
http://blog.csdn.net/zhaihaohao1/article/details/41544087
http://blog.csdn.net/zhaihaohao1/article/details/42971149