核心代码:
package cn.zengfansheng.camera;
import java.io.File;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.widget.ImageView;
public class MainActivity extends Activity {
private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
private ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (ImageView) this.findViewById(R.id.iv);
}
public void capture(View view){
Intent intent = new Intent();
intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);// android.media.action.IMAGE_CAPTURE
// intent.setAction("android.media.action.IMAGE_CAPTURE");
intent.addCategory(Intent.CATEGORY_DEFAULT);
String fileUri = Environment.getExternalStorageDirectory()+"/haha"+System.currentTimeMillis()+".jpg";
intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(new File(fileUri)));// output
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (data != null) {
Bitmap bitmap = data.getParcelableExtra("data");// 查看系统源码,可以知道返回的是这样一个数据,不过这是缩略图。
iv.setImageBitmap(bitmap);
}
super.onActivityResult(requestCode, resultCode, data);
}
} |