首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Android使用摄像头意图并在本地保存图像

Android使用摄像头意图并在本地保存图像
EN

Stack Overflow用户
提问于 2018-06-09 05:22:52
回答 1查看 42关注 0票数 1

我目前正在尝试创建一个按钮,用于打开相机应用程序,并使用特定的名称和位置保存图片。

这用于创建名称/目录

代码语言:javascript
复制
// 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;
    }

这是我的接头人

代码语言:javascript
复制
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);
        }
    }



}

我的问题是,在"STOP“之后,它会在后面添加一个19位数字。我不知道为什么会这样,建议?

编辑:

文件提供程序

代码语言:javascript
复制
<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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-09 06:06:27

这是因为createTempFile在它的定义中有这样一行:

代码语言:javascript
复制
...new File(tmpDirFile, prefix + tempFileRandom.nextInt() + suffix);

我认为这是导致问题的原因。只需使用new File(...)

代码语言:javascript
复制
File image = new File(storageDir, Calib_image_File_Name + ".jpg");
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50768551

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档