图像保存到SD与当前日期和时间在名义上不工作 [英] Saving image to sd with current date and time in name doesn't work
本文介绍了图像保存到SD与当前日期和时间在名义上不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的应用程序使用一个按钮来启动相机应用程序并保存图片到特定的文件夹上按SD卡的当前日期和时间命名。当我辛苦code中的名称为图像,它工作正常,但如果我试图把迄今它没有在所有的工作名。
In my application I use a button to launch Camera application and save picture to specific folder on sdCard naming it by current date and time. When I hardcode the name for the picture, it works fine, but if I'm trying to put date in the name it doesn't work at all.
Intent imageIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File imagesFolder = new File(Environment.getExternalStorageDirectory(), Constants.IMAGE_FOLDER_URI);
imagesFolder.mkdirs();
Date d = new Date();
CharSequence s = DateFormat.format("MM-dd-yy hh:mm:ss", d.getTime());
File image = new File(imagesFolder, s.toString() + ".jpg"); //this line doesn't work
Uri uriSavedImage = Uri.fromFile(image);
imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
startActivity(imageIntent);
如果我把:
s = "some_name";
那么它的工作原理,但我需要在映像名称当前日期和时间。
then it works, but I need current date and time in image name.
推荐答案
大肠:
未在文件名中的有效字符,这就是为什么它失败创建这样的文件。
试着改变你的名字模式是这样的:
Colon :
is not a valid character in a file name, that is why it is failing to create such a file.
Try change your name pattern to something like this:
CharSequence s = DateFormat.format("MM-dd-yy hh-mm-ss", d.getTime());
这篇关于图像保存到SD与当前日期和时间在名义上不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文