per*_*000 6 android android-intent android-camera android-camera-intent android-file
我在尝试通过意图拍照时遇到一个非常奇怪的错误。活动代码(略有简化):
private void startCapture() throws IOException {
// Create output file
final File photoFile = makePhotoFile();
_photoPath = photoFile.getAbsolutePath();
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
/* IMPORTANT NOTE TO READERS
* As of Android N (which went out shortly after I posted this question),
* you cannot use Uri.fromFile this way anymore.
* You should use a FileProvider. */
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
if (cameraIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(cameraIntent, REQUEST_CODE_IMAGE_CAPTURE);
}
else {
deleteCurrentPhoto();
}
}
private File makePhotoFile() throws IOException {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.ENGLISH).format(new Date());
String imageFileName = "MyPhoto_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
return File.createTempFile(imageFileName, ".jpg", storageDir);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == REQUEST_CODE_IMAGE_CAPTURE) {
if(resultCode == RESULT_OK) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
final byte[] buffer = new byte[2048];
int read;
byte[] photoBytes;
try(FileInputStream fis = new FileInputStream(_photoPath)) {
for(int i=0; i<10; i++) { // Always working after first iteration though
if( (read = fis.read(buffer)) <= 0) {
// Why does this get printed once ??
Log.w("my.package.my.app", "Empty for now...");
}
else {
Log.w("my.package.my.app", "Working now !");
bos.write(buffer, 0, read);
break;
}
}
while((read = fis.read(buffer)) >= 0) {
bos.write(buffer, 0, read);
}
photoBytes = bos.toByteArray();
} // Catch clauses removed for simplicity
// Everything working OK after that (photoBytes decodes fine with the BitmapFactory)
}
}
}
Run Code Online (Sandbox Code Playgroud)
和日志输出:
03-01 16:32:34.139 23414-23414 / my.package.my.app W / my.package.my.app:现在为空... 03-01 16:32:34.139 23414-23414 / my.package .my.app W / my.package.my.app:现在可以使用!
如您所见,在拍照后在onActivityResult中,第一次调用FileInputStream.read ...时文件为空,然后可以正确读取它!我认为,当摄像机意图返回到调用活动时,该文件将已经写入。有某种延迟吗?我也很惊讶地看到它在for循环中经过一轮迭代后一直可以正常工作。
请注意,如果在onActivityResult的开头放置一个断点,则会延迟执行时间,并且一切正常(第一次尝试即可正确读取文件)。
我在库存相机应用中使用的是Android 6.0.1下的库存Nexus 6P。
重要修改:至于Android N,您应该使用FileProvider而不是Uri.fromFile以前的版本。请参阅此博客文章以及Android团队的这些说明。
不要已经创建该文件。去除File.createTempFile()。
您唯一需要的是文件名。一个文件路径。相机应用程序将自行创建该文件。因此,更改您的函数来创建新文件名。例如new File(storageDir, imageFileName + ".jpg")