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”)intent.putExtra(MediaStore.EXTRA_OUTPUT,“new photo name.jpg”)