1

我目前正在尝试将相机集成到我的 android 应用程序中并将图片保存在我的设备上;但是,当我拍照时,我无法继续(该应用程序可以运行,但相机镜头冻结意味着我可以再拍一张照片并返回,但无法确认)。我想知道你们是否可以帮忙。请尽可能简单地解释这一点,因为我对 android 编程非常陌生。感谢大家!

public class PhotoCapture extends Activity {
int TAKE_PHOTO_CODE = 0;
public static int count=0;
public static File newfile;
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.photocapture);



Button capture = (Button) findViewById(R.id.takepicture);
capture.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {

        ContextWrapper cw = new ContextWrapper(getBaseContext());
         newfile = cw.getDir("test.jpg", Context.MODE_PRIVATE);

        try {
            newfile.createNewFile();
        } catch (IOException e) {}       

        Uri outputFileUri = Uri.fromFile(newfile);


        Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
        cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);

      startActivityForResult(cameraIntent, TAKE_PHOTO_CODE);
    }
});
}

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

    if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK) {


         uri = data.getData().toString();
        Log.d("CameraDemo", "Pic saved");

        Intent myview = new Intent(this, Finalpiece.class);
        startActivity(myview);


     }
}
}
4

1 回答 1

1
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
 <uses-permission android:name="android.permission.CAMERA"/>

使用此权限之前...

private static final int REQ_CAPTURE_CAPTURE__IMAGE = 0;
    private String imagePath;

/**
     * Start camera activity.
     */
    protected void startCameraActivity() {
        String imageDirectory = Environment.getExternalStorageDirectory()
        + File.separator + "surbeyImg";
        File imageDirectory = new File(imageDirectory);

        if (!(imageDirectory.exists())) {
            imageDirectory.mkdir();
        }

        String imagePath = imageDirectory + File.separator + imgName+".jpg");   

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

        Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

        intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
        intent.putExtra("outputFormat", Bitmap.CompressFormat.PNG.toString());
        startActivityForResult(intent, REQ_CAPTURE_CAPTURE__IMAGE);
    }

    protected void onActivityResult(int requestCode, int resultCode,
            Intent imageReturnedIntent) {

        switch (requestCode) {

        case REQ_CAPTURE_CAPTURE__IMAGE:

            if (resultCode == RESULT_OK) {
                Intent intentImg = new Intent(CameraDemo.this, ShowImg.class);             
                intentImg.putExtra("imagePath",imagePath);         
                startActivity(intentImg); 

            }

            break;

        default:
            break;

        }
    }

在此之后使用此代码...谢谢

于 2013-04-23T04:53:33.913 回答