捕获图像不工作的Android手机

时间:2015-06-22 06:44:08

标签: android

我正在通过相机拍摄图像。 我开始打算捕捉图像。但是在捕获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;
    }

1 个答案:

答案 0 :(得分:0)

EXTRA_OUTPUT不是文件。它需要是内容提供者URI。我无法从您的代码中告知存储它的位置,但它肯定不能存储在您应用的私人目录中 - 其他意图在那里没有写入权限。除非您想为此实现完整的内容提供程序,否则不要将其传递给输出URI并让它选择文件名。或者请参阅http://developer.android.com/training/camera/photobasics.html以获取示例代码。你的不仅仅是笨拙。