2

按照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>
4

2 回答 2

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);

}

然后在这里将位图像这样放在图像视图中

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())));





        }
于 2013-03-11T23:48:49.283 回答
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));

于 2013-03-11T23:45:03.137 回答