一: Camara应用程序包含的意图过滤器
<intent-filter>
<action android:name="android.media.action.IMAGE_CAPTURE"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
通过意图利用Camara应用程序
Intent it = new Intent("android.media.action.IMAGE_CAPTURE");
startActivity(it);
注意:建议使用下面的方式
Intent it = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivity(it);
(不容易出错)
当需要捕获图片时
Intent it = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(it);
就可以从Camara应该程序中获得返回数据,数据的类型为Bitmap
获得Bitmap代码
Bundle bundle = data.getExtras();
Bitmap bitmap = (Bitmap) bundle.get("data");
package com.camaraintent_a;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
private Button btnIntentCamara;
private ImageView ivReturnBitmap;
private final static int requestCode = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnIntentCamara = (Button) findViewById(R.id.btn_intent_camara);
ivReturnBitmap = (ImageView) findViewById(R.id.iv_return_bitmap);
btnIntentCamara.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/*Intent it = new Intent("android.media.action.IMAGE_CAPTURE");*/
/*Intent it = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivity(it);*/
Intent it = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(it, requestCode);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode==this.requestCode) {
if (resultCode==this.RESULT_OK) {
Bundle bundle = data.getExtras();
Bitmap bitmap = (Bitmap) bundle.get("data");
ivReturnBitmap.setImageBitmap(bitmap);
}
}
}
}