Java 使用照相机时给图像命名
在我的应用程序中,我打开相机并想用特定的名称保存该文件。 我使用以下代码:Java 使用照相机时给图像命名,java,android,image,save,mediastore,Java,Android,Image,Save,Mediastore,在我的应用程序中,我打开相机并想用特定的名称保存该文件。 我使用以下代码: public void onLongPress(MotionEvent e) { // TODO Auto-generated method stub Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); cameraIntent.putExtra(MediaS
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File("new-photo-name.jpg")) );
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
}
protected void onActivityResult1(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_PIC_REQUEST) {
Bitmap image = (Bitmap) data.getExtras().get("data");
}
}
它确实打开了相机,我可以拍摄并保存照片,但它没有给出好的名字。
每次我保存图片时,他都会给图片起另一个名字,例如:“1333675392558.jpg”。我不明白他怎么会有这样的数字
为什么我的代码没有应用名称:“newphotoname.jpg”
和/或我做错了什么
已经谢谢你了,Bigflow
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
设置文件名的行。这条线你必须换
设置文件名的行。这是您必须更改的行。我让它工作了,但还不知道完全相同的问题,但这段代码对我有效:
private Uri outputFileUri;
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file = new File(Environment.getExternalStorageDirectory(), "/DCIM/Camera/new-photo-name.jpg");
outputFileUri = Uri.fromFile(file);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, TAKE_PICTURE);
}
onLongPress与手势(触摸)动作有关,您也可以在此处使用按钮
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == TAKE_PICTURE){
System.out.println("string of file name = "+outputFileUri.toString());
}
}
非常小的代码,但工作起来很有魅力我让它工作了,但还不知道完全相同的问题,但这段代码对我来说很有效:
private Uri outputFileUri;
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file = new File(Environment.getExternalStorageDirectory(), "/DCIM/Camera/new-photo-name.jpg");
outputFileUri = Uri.fromFile(file);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, TAKE_PICTURE);
}
onLongPress与手势(触摸)动作有关,您也可以在此处使用按钮
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == TAKE_PICTURE){
System.out.println("string of file name = "+outputFileUri.toString());
}
}
非常小的代码,但工作起来很有魅力将其更改为:
intent.putExtra(MediaStore.EXTRA_输出,“new photo name.jpg”)但不知怎么的,这不起作用。字符串不是Uri。我不知道这是否是最好的,我的应用程序使用了Uri.fromFile(新文件(字符串文件名)),我更改了代码,所以它更容易查看。但这也不起作用:(我刚刚注意到你的代码中有“onActivityResult1”。这里真的有“1”吗?如果有,请删除它,然后重试:)哦,我删除了它,但不知怎么的,它仍然不起作用。我认为代码中的其他地方出了问题,我现在将搜索该问题。将其更改为:intent.putExtra(MediaStore.EXTRA_OUTPUT,“new photo name.jpg”)但不知怎么的,这不起作用。字符串不是Uri。我不知道这是否是最好的,我的应用程序使用了Uri.fromFile(新文件(字符串文件名)),我更改了代码,所以它更容易查看。但这也不起作用:(我刚刚注意到你的代码中有“onActivityResult1”。这里真的有“1”吗?如果有,请删除它,然后重试:)哦,我删除了它,但不知怎么的,它仍然不起作用。我认为它在代码中的其他地方出错了,我现在将搜索问题。