[英]Why pictures taken by my app are 0 size in Android?
Pictures taken by my app, in Windows Explorer have always 0 bytes and can't be opened. 我的应用在Windows资源管理器中拍摄的照片始终为0字节,无法打开。 I can only see them in Android Gallery app. 我只能在Android Gallery应用中看到它们。
I am taking photo with this method : 我正在用这种方法拍照:
private void takePhoto() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
File photoFile = null;
try {
photoFile = PhotoUtils.createImageFile(this);
} catch (IOException ex) {
Log.v("MainActivity", "Creating image exception");
}
if (photoFile != null) {
fileUri = Uri.fromFile(photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
}
And this is PhotoUtils.createImageFile(...)
: 这是PhotoUtils.createImageFile(...)
:
public static File createImageFile(Context context) throws IOException {
String currentPhotoPath;
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "ES_" + timeStamp;
File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(imageFileName, ".jpg", storageDir);
currentPhotoPath = image.getAbsolutePath();
galleryAddPic(context, currentPhotoPath);
return image;
}
public static void galleryAddPic(Context context, String currentPhotoPath) {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(currentPhotoPath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
context.sendBroadcast(mediaScanIntent);
}
You are indexing the file when it is zero bytes long, in the form of your temp file. 您正在以临时文件的形式为长度为零字节的文件建立索引。 Try moving your galleryAddPic()
call until after the picture has been taken. 尝试移动您的galleryAddPic()
调用,直到拍摄完照片为止。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.