关注the android tutorial,我可以拍照。
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>
答案 0 :(得分:1)
像这样:
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;
ImageView image;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_take_photo);
// Show the Up button in the action bar.
setupActionBar();
image = (ImageView)findViewById(R.id.imageView1);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri();
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
然后在这里将位图放在imageview中,就像这样
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == 100) {
try {
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), fileUri);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
image.setImageBitmap(bitmap);
sendBroadcast(new Intent(
Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://" + Environment.getExternalStorageDirectory())));
}
答案 1 :(得分:0)
Bitmap b = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri)
myview.setBackground(new BitmapDrawable(b));
“myview”是布局中的任何视图对象。因此,如果您的布局包含ID为“@ + id / myview”的RelativeLayout对象,那么第二行可以说:
findViewById(R.id.myview).setBackground(new BitmapDrawable(b));