5

我正在使用手机的摄像头并使用 android.provider.MediaStore.ACTION_IMAGE_CAPTURE 从我的应用程序中捕获图像。

单击捕获按钮后,它显示保存或丢弃。单击保存按钮,它会返回相机而不是应用程序。并且图像保存在画廊中,而不是我声明的输出文件中。

下面是我的代码

    @Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    Intent in=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(in, 0);

}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode==RESULT_OK) {
    Bitmap bm=(Bitmap) data.getExtras().get("data");
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    bm.compress(Bitmap.CompressFormat.JPEG, 40, bytes);

    //you can create a new file name "test.jpg" in sdcard folder.
    File f = new File(Environment.getExternalStorageDirectory()
                            + File.separator + "player1.jpg");
    try {
        f.createNewFile();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    //write the bytes in file
    FileOutputStream fo = null;
    try {
        fo = new FileOutputStream(f);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        fo.write(bytes.toByteArray());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }finally{
        Intent pic=new Intent(pic1.this, GameMenu.class);
        startActivity(pic);
        finish();
    }
}
else {
    Toast.makeText(getApplicationContext(), "Canceled", Toast.LENGTH_LONG).show();
    Intent pic=new Intent(pic1.this, Menu.class);
    startActivity(pic);
    finish();
}

请提出任何解决方案..提前谢谢...

编辑 1:我在 samsung galaxy y duos (android 2.3.6) 上检查了这段代码,它不能正常工作。并在 Galaxy pop(2.2.1) 和 HTC wildfire(2.3.5) 上进行了同样的检查,其工作完美。

4

5 回答 5

2

使用以下意图捕获图像,getTempFile()是您提到存储路径的位置

Intent photoPickerIntent= new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                       photoPickerIntent.putExtra(MediaStore.EXTRA_OUTPUT, getTempFile());

                       photoPickerIntent.putExtra("outputFormat",Bitmap.CompressFormat.JPEG.toString());
                       photoPickerIntent.putExtra("return-data", true);
                       startActivityForResult(photoPickerIntent,TAKE_PICTURE);

获取临时文件:

private Uri getTempFile() {


        File root = new File(Environment.getExternalStorageDirectory(), "Directory");
        if (!root.exists()) {
            root.mkdirs();
        }
             File file;

             file = new File(root,filename+".jpeg" );

             muri = Uri.fromFile(file);
             photopath=muri.getPath();

              Log.e("getpath",muri.getPath());
              return muri;

           }

阅读此处了解更多主题

于 2012-08-29T07:05:46.340 回答
0

如果这是你的全部代码,你已经省略了 setContentView(); 可能是它没有返回的原因。

  @Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
     // setContentView() here
    Intent in=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(in, 0);

}

是否调用了 onActivityResult() 方法?

于 2012-08-29T07:06:14.027 回答
0

您应该创建打开相机的文件。您将文件 URI 作为相机意图的额外内容:

/**
 * Capturing Camera Image will lunch camera app request image capture
 */
private void captureImage() {

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    fileUri = getOutputMediaFileUri();

    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);

    // start the image capture Intent
    startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
}

这就是我创建文件的方式:

/**
 * Creating file uri to store image
 * 
 * 
 * @return Uri
 */
public Uri getOutputMediaFileUri() {
    return Uri.fromFile(getTempOutputMediaFile());
}

/**
 * returning File
 * 
 * 
 * @return File
 */
private File getTempOutputMediaFile() {

    File appDirectory = this.getFilesDir();

    File mediaStorageDir = new File(APP_NAME,
                YOUR_IMAGE_DIRECTORY_NAME);

    Log.i("mediaStorageDir", mediaStorageDir.getAbsolutePath());

    // Create the storage directory if it does not exist
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            Log.i(YOUR_IMAGE_DIRECTORY_NAME,
                    "Oops! Failed create "
                            + YOUR_IMAGE_DIRECTORY_NAME
                            + " directory");
            return null;
        }
    }

    // Create a media file name
    UUID temporaryImageUuid = UUID.randomUUID();
    File mediaFile;

    mediaFile = new File(mediaStorageDir.getPath() + File.separator
            + temporaryImageUuid + "." + YOUR_EXTENSION);

    return mediaFile;
}
于 2014-04-14T23:09:34.217 回答
0

嗨检查以下代码。捕获图像后使用 startPreview() 释放。它适用于我

        captureButton.setOnClickListener(new View.OnClickListener() {
                        @Override
            public void onClick(View v) {
                            mCamera.autoFocus(null);
                mCamera.takePicture(null, null, mPicture);
                Log.e("in capture button","onclick of this button"+ mCamera);
                mCamera.startPreview();
                                }
        });
于 2014-04-15T10:39:43.227 回答
0

我遇到了同样的问题,我已经重新启动了我的设备并测试了我的应用程序,它现在可以正常工作了。不同的代码实现应用程序可能没有响应。尝试重新启动您的设备并进行测试,让我知道它是否正常工作

于 2012-11-09T05:06:17.913 回答