捕获的图像不能正常工作android手机
问题描述:
我正在通过相机捕获图像。 我开始意图捕捉图像。但在捕获2 3次摄像头捕捉不工作和文件创建的长度为0. 以下是我用于此任务的代码。捕获的图像不能正常工作android手机
@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;
}
答
EXTRA_OUTPUT不是文件。它需要是一个内容提供者URI。我无法从你的代码中知道它被存储在哪里,但它绝对不能存在于你的应用程序的私人目录中 - 另一个意图在那里没有写权限。除非你想为此实现一个完整的内容提供者,否则不要传递一个输出URI并让它选择一个文件名。例如代码见http://developer.android.com/training/camera/photobasics.html。你的情况有点笨拙。
+0
感谢您的回复。我解决了它。当我用照相机拍摄照片时,我的错误是我的活动正在死亡。我正在使用oncreate()中的系统时间创建文件名。由于我的活动正在进行中,它再次调用活动的所有生命周期,并且我的目标文件名正在发生变化。所以我对文件名创建进行了更改并解决了我的问题 – yogesh
因为你的uri是错误的,请检查文件夹是否先创建 –
感谢您的回复。我解决了我的问题 – yogesh
感谢您的回复。我解决了它。当我用照相机拍摄照片时,我的错误是我的活动正在死亡。我正在使用oncreate()中的系统时间创建文件名。由于我的活动正在进行中,它再次调用活动的所有生命周期,并且我的目标文件名正在发生变化。所以我对文件名创建进行了更改并解决了我的问题 – yogesh