按照android教程,我可以拍照。
public class TakePhotoActivity extends Activity {
private Uri fileUri;
// I am not sure what this is for yet
private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_take_photo);
// Show the Up button in the action bar.
setupActionBar();
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri();
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
private static Uri getOutputMediaFileUri() {
return Uri.fromFile(getOutputMediaFile());
}
private static File getOutputMediaFile() {
File mediaStorageDir = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
"MyCameraApp");
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d("MyCameraApp", "failed to create directory");
return null;
}
}
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
.format(new Date());
return new File(mediaStorageDir.getPath() + File.separator + "IMG_"
+ timeStamp + ".jpg");
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE == requestCode) {
if (RESULT_OK == resultCode) {
// Toast.makeText(this, "Image saved to:\n" + data.getData(),
// Toast.LENGTH_LONG).show();
}
}
}
}
现在我想显示我刚拍的照片,使用onActivityResult
. 有没有人有一些代码可以做到这一点?
编辑以显示 XML:
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".TakePhotoActivity" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/desc" />
</RelativeLayout>