我正在通過相機捕獲圖像。 我開始意圖捕捉圖像。但在捕獲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;
}
因爲你的uri是錯誤的,請檢查文件夾是否先創建 –
感謝您的回覆。我解決了我的問題 – yogesh
感謝您的回覆。我解決了它。當我用照相機拍攝照片時,我的錯誤是我的活動正在死亡。我正在使用oncreate()中的系統時間創建文件名。由於我的活動正在進行中,它再次調用活動的所有生命週期,並且我的目標文件名正在發生變化。所以我對文件名創建進行了更改並解決了我的問題 – yogesh