相机在Android应用程序上冻结

时间:2013-04-23 03:46:45

标签: android android-intent android-camera

我正在尝试将相机集成到我的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);


     }
}
}

1 个答案:

答案 0 :(得分: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;

        }
    }

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