今天学了安卓程序的拍照功能,在网上学了相关的内容,写了简单的代码实现,明天在程序中加入,和其他代码整合。
import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.os.Bundle; import android.provider.MediaStore; import android.util.Log; public class PhotographActivity extends Activity { /** Called when the activity is first created. */ private String logTag = "Exception"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); try { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(intent, 0); } catch (Exception e) { Log.v(logTag, e.getMessage()); } } protected void onActivityResult(int requestCode, int resultCode, Intent data) { try { if (requestCode != 0) { return; } super.onActivityResult(requestCode, resultCode, data); Bundle extras = data.getExtras(); Bitmap b = (Bitmap) extras.get("data"); Intent intent = new Intent(); //得到图片后,我们在另一个activity中显示 intent.setClass(this, ShowImageActivity.class); intent.putExtra("image",b); this.startActivity(intent); } catch (Exception e) { // TODO: handle exception Log.v(logTag, e.getMessage()); } } } 新建一个显示图片的activity。这样,在这个activity中就可以对图片做其他更多的操作。 import android.app.Activity; import android.graphics.Bitmap; import android.os.Bundle; import android.util.Log; import android.widget.ImageView; public class ShowImageActivity extends Activity { private String logTag = "exception"; private ImageView view; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.show); try { view = (ImageView) findViewById(R.id.view); Bundle bundle = this.getIntent().getExtras(); Bitmap b = bundle.getParcelable("image"); view.setImageBitmap(b); //setContentView(view); } catch (Exception e) { Log.v(logTag, e.getMessage()); throw new RuntimeException(e); } } }