Android 2.2 SDK - PhotoCapture示例无法正常工作

时间:2010-12-30 17:22:21

标签: android mobile android-emulator android-2.2-froyo

我正在整合来自此处的确切代码:http://labs.makemachine.net/2010/03/simple-android-photo-capture/

活动应如何运作:点击“按钮” - >转到默认相机。拍照。 “重新拍摄”按钮工作,“取消”按钮工作(返回“按钮”布局),但“确定”按钮不起作用(然后图像应出现在上一个布局中的“按钮”上方)。这与它如何将图像保存到SD卡有关吗?我想不出来!此外,我正在设备上测试此应用程序。

1 个答案:

答案 0 :(得分:1)

让它发挥作用:

package com.android.xxx;

import java.io.File;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.Window;

public class CameraView extends MenusHolder {

    protected String _path;
    protected boolean _taken;

    protected static final String PHOTO_TAKEN = "photo_taken";

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.create_event_view);

        File imageDirectory = new File(Environment.getExternalStorageDirectory() + "/MyFolder/");
        imageDirectory.mkdirs();

        _path = Environment.getExternalStorageDirectory() + "/MyFolder/temporary_holder.jpg"; 

        startCameraActivity();

    }

    protected void startCameraActivity() {

        File file = new File(_path);
        Uri outputFileUri = Uri.fromFile(file);

        Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);

        startActivityForResult(intent, 0);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        switch(resultCode) {

            case 0:
                finish();
                break;

            case -1:
                onPhotoTaken();
                break;

        }

    }

    protected void onPhotoTaken() {

        _taken = true;
        finish();
        Intent newView1 = new Intent(CameraView.this, CreateEventView.class);
        CameraView.this.startActivity(newView1);

    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {

        outState.putBoolean(CameraView.PHOTO_TAKEN, _taken);  

    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {

        if (savedInstanceState.getBoolean(CameraView.PHOTO_TAKEN)) {

            onPhotoTaken();

        }

    }

}