简体   繁体   English

使用意图相机时给图像起名字

[英]Give name to image when used intent camera

In my app, I open the camera and want to save that file with a specific name. 在我的应用中,我打开相机,并希望使用特定名称保存该文件。 I use this code: 我使用以下代码:

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");  
                    }   
}

It does open the camera, I can take and save the photo, but it does not give the good name. 它确实可以打开相机,我可以拍摄并保存照片,但是并没有给出好名字。 Everytime when I save the picture, he gives the picture an other name, 1 name example is: "13333675392558.jpg". 每次保存图片时,他都会给图片起一个其他名称,其中一个名称示例是:“ 13333675392558558.jpg”。 I don't understand how he comes with that kind of numbers. 我不明白他是怎么得到这种数字的。

Why does my code does not apply the name: "new-photo-name.jpg" ? 为什么我的代码没有应用名称:“ new-photo-name.jpg”?

And/Or what do I wrong then? 和/或那我怎么办?

Thanks already, Bigflow 已经感谢Bigflow

intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);

is the line that sets the filename. 是设置文件名的行。 It's this line you must change. 您必须更改此行。

I got it working, but don't know the exact same problem yet, but this code worked for me: 我可以使用它,但是还不知道完全相同的问题,但是这段代码对我有用:

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 has something to do with gesture (touch) actions, you could also use a button here. onLongPress与手势(触摸)动作有关,您也可以在此处使用按钮。

public void onActivityResult(int requestCode, int resultCode, Intent data) {
      if (requestCode == TAKE_PICTURE){
            System.out.println("string of file name = "+outputFileUri.toString());
      }
}

Really small code, but works like a charm 很小的代码,但是像个魅力

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM