简体   繁体   English

Android使用相机意图并保存图像本地化

[英]Android using camera intent and saving image localy

I am currently trying to create a button the opens the camera app, and saves the picture with a specific name and location. 我目前正在尝试创建一个按钮,打开相机应用程序,并使用特定的名称和位置保存图片。

this is used to create the name/directory 这用于创建名称/目录

// create a filename for image to be stored into    
    private File createImageFile() throws IOException {
        // Create an image file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String Calib_image_File_Name = "Calibration" + "_" + timeStamp + "STOP";
        File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
        File image = File.createTempFile(
                Calib_image_File_Name,  /* prefix */
            ".jpg",         /* suffix */
            storageDir      /* directory */
        );

        // Save a file: path for use with ACTION_VIEW intents
        mCurrentPhotoPath = image.getAbsolutePath();
        return image;
    }

And this is my handler 这是我的经理

public void Take_Image_calib(View view)
{
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    // Ensure that there's a camera activity to handle the intent
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        // Create the File where the photo should go
        File photoFile = null;
        try {
            photoFile = createImageFile();
        } catch (IOException ex) {
            // Error occurred while creating the File
           // ...
        }
        // Continue only if the File was successfully created
        if (photoFile != null) {
            Uri photoURI = FileProvider.getUriForFile(this,
                                                  "com.example.android.fileprovider",
                                                  photoFile);
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
            startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
        }
    }



}

The issue i have is that after the "STOP" , it adds a 19 digit number afterwards. 我的问题是,在“ STOP”之后,其后会添加一个19位数字。 i don't know why this is happening, advice? 我不知道为什么会这样,建议?

EDIT: 编辑:

File provider 文件提供者

<provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="com.example.android.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths"></meta-data>
    </provider>

This is happening because of the createTempFile in its definition it has this line: 发生这种情况是由于其定义中的createTempFile具有以下行:

...new File(tmpDirFile, prefix + tempFileRandom.nextInt() + suffix);

which I believe causes the problem. 我相信会导致问题。 Simply use new File(...) : 只需使用new File(...)

File image = new File(storageDir, Calib_image_File_Name + ".jpg");

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

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