捕获图像无法使用android手机

捕获图像无法使用android手机,android,Android,我正在用相机拍摄图像。 我开始打算捕捉图像。但在捕获2 3次后,相机捕获不起作用,创建的文件长度为0。 下面是我用于此任务的代码 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); capturePhotoForRecordUpload(); } public synchronized vo

我正在用相机拍摄图像。 我开始打算捕捉图像。但在捕获2 3次后,相机捕获不起作用,创建的文件长度为0。 下面是我用于此任务的代码

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        capturePhotoForRecordUpload();
        }


 public synchronized void capturePhotoForRecordUpload() {

        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        // Ensure that there's a camera activity to handle the intent
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            // Create the File where the photo should go
            File photoFile = null;
            try {
                photoFile = createFileForNewRecordImage();
            } catch (IOException ex) {
                // Error occurred while creating the File
                Debug.printException(ex);
            }
            // Continue only if the File was successfully created
            if (photoFile != null) {
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                        Uri.fromFile(photoFile));
                startActivityForResult(takePictureIntent, 100);
            }
        }
    }



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

        String errorString = "";


        if (resultCode == RESULT_OK) {
                //startLoading();
                mCurrentPhotoPath = Common.getSharedPreference(CaptureRecordActivity.this, "mCurrentPhotoPath");
                if(new File(mCurrentPhotoPath).exists() && new File(mCurrentPhotoPath).length() > 0){
                 // here i upload the record

            }else{
                errorString = "Error while uploading file. Please try again.";
            }

        }else{

        }

        /// display the toast  of errorString here 

       }

        private  File createFileForNewRecordImage() throws IOException {
        // Create an image file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "JPEG_" + timeStamp + "_";
        File storageDir = new File(Common.getTempUploadDirectoryPath());
        if(!storageDir.exists())
            storageDir.mkdirs();
        if(!storageDir.exists())
            storageDir.mkdir();

    // I also checked this by removing the below lines
       /* File image = File.createTempFile(
                imageFileName,  /* prefix */
                ".jpg",         /* suffix */
                storageDir      /* directory */
        );*/

        // Save a file: path for use with ACTION_VIEW intents
        mCurrentPhotoPath = image.getAbsolutePath();
        Common.updateSharedPreference(this, "mCurrentPhotoPath", mCurrentPhotoPath);
        return image;
    }

额外的_输出不是文件。它需要是一个内容提供者URI。我无法从你的代码中判断它存储在哪里,但它肯定不可能在你的应用程序的私有目录中——另一个意图是在那里没有写权限。除非您希望为此实现完整内容提供程序,否则不要向其传递输出URI并让其选择文件名。或者参见示例代码。您的文件夹有点笨重。

这是因为您的uri错误,请先检查文件夹是否已创建。谢谢回复。我解决了我的问题谢谢你的回答。我解决了。当我用相机拍摄照片时,我犯了错误,我的活动被杀死了。我在oncreate()中使用系统时间创建文件名。当我的活动再次被杀死时,请调用活动的所有生命周期,我的目标文件名将被更改。因此,我在创建文件名时进行了更改,并解决了我的问题。谢谢您的回复。我解决了。当我用相机拍摄照片时,我犯了错误,我的活动被杀死了。我在oncreate()中使用系统时间创建文件名。当我的活动再次被杀死时,请调用活动的所有生命周期,我的目标文件名将被更改。因此,我在创建文件名时进行了更改,并解决了我的问题