在牛轧糖中从相机拍摄时无法加载图像 [英] Image not loading when capturing from the camera in nougat

查看:61
本文介绍了在牛轧糖中从相机拍摄时无法加载图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从零开始捕获图像时,我正在调用此方法。

I am calling this method when capturing image from the nought.

private void CallCameraFeature() {    
    Intent cameraOpeningIntent = new 
    Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {      
        String fileName = EmpConstants.startImgName +
                    new SimpleDateFormat(EmpConstants.PhotoFileFormat, 
                    Locale.US).format(new Date());
        File imgFile = new File(mContext.getFilesDir(), "images");
        File outFile = new File(imgFile, fileName + ".jpg");
        Uri photoURi = FileProvider.getUriForFile(mContext, 
        BuildConfig.APPLICATION_ID + ".provider", outFile);
        cameraOpeningIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURi);
        startActivityForResult(cameraOpeningIntent, REQUEST_IMAGE_CAPTURE);
    }

}

我已经在


values-> provider_paths.xml

values -> provider_paths.xml

在此路径中存储图像

provider_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths >
<files-path name="my_images" path="images/"/>
<files-path name="my_docs" path="docs/"/>
</paths>

定义为存储图像DCIM的路径。

Path defined like this to store image DCIM.

public String getEmpThumbImageDirPath() {
    try {
        return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_
        DCIM).toString() + EmpConstants.appDir;  
    }catch (Exception e) {
        Log.d("eEmp/ImgDir", e.toString());
        return "";
    }

相机正在打开并捕获图像,但图像未加载。我犯了什么错误。

camera is opening and capturing the image but image is not loading. What mistake I have done.

任何帮助将不胜感激。

04-13 20:05:43.738 30272-30272 / com.efftronics.android.eEmployee E / ContentValues:createImageFile:目录已成功创建。
04-13 20:05:43.739 30272-30272 / com.efftronics.android.eEmployee E / ContentValues:运行:图像文件夹路径为:/ storage / emulated / 0 / FolderName / InsideFolderNameIFYOUWant
04- 13 20:05:43.739 30272-30272 / com.efftronics.android.eEmployee E / ContentValues:createImageFile:图像文件名是:imageName_1523630143739
04-13 20:05:49.791 30272-30272 / com.efftronics.android .eEmployee E / ContentValues:createImageFile:目录已存在。
04-13 20:05:49.792 30272-30272 / com.efftronics.android.eEmployee E / ContentValues:运行:图像文件夹路径为:/ storage / emulated / 0 / FolderName / InsideFolderNameIFYOUWant
04- 13 20:05:49.792 30272-30272 / com.efftronics.android.eEmployee E / ContentValues:createImageFile:图像文件名是:imageName_1523630149792

04-13 20:05:43.738 30272-30272/com.efftronics.android.eEmployee E/ContentValues: createImageFile: directory was created successfully. 04-13 20:05:43.739 30272-30272/com.efftronics.android.eEmployee E/ContentValues: run: image folder path is: /storage/emulated/0/FolderName/InsideFolderNameIFYOUWant 04-13 20:05:43.739 30272-30272/com.efftronics.android.eEmployee E/ContentValues: createImageFile: image file name is: imageName_1523630143739 04-13 20:05:49.791 30272-30272/com.efftronics.android.eEmployee E/ContentValues: createImageFile: directory already exists. 04-13 20:05:49.792 30272-30272/com.efftronics.android.eEmployee E/ContentValues: run: image folder path is: /storage/emulated/0/FolderName/InsideFolderNameIFYOUWant 04-13 20:05:49.792 30272-30272/com.efftronics.android.eEmployee E/ContentValues: createImageFile: image file name is: imageName_1523630149792

推荐答案

尝试一下,它对我有用:

Try this, It works for me:

private void openCamera()
    {
        try
        {
            Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

            // Ensure that there's a camera activity to handle the intent
            if (takePictureIntent.resolveActivity(getPackageManager()) != null)
            {
                try
                {
                    // Create the File where the photo should go
                    File photoFile = createImageFile();

                    // 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, CAPTURE_IMAGE_REQUEST_CODE);
                    }
                    else
                    {
                        Log.e(TAG, "openCamera: image was not captured.");
                    }
                }
                catch (Exception ex)
                {
                    // Eor occurred while creating the File
                    ex.printStackTrace();
                }
            }
        }
        catch (Exception e)
        {
            Log.e(TAG, "openCamera: exception while opening camera:");
            e.printStackTrace();
        }
    }

private String mCurrentPhotoPath;

private File createImageFile()
    {
        // Create an image file name
        File sd = Environment.getExternalStorageDirectory();

        File imageFolder = new File(sd.getAbsolutePath() + File.separator +
                "FolderName" + File.separator + "InsideFolderNameIFYOUWant");

        if (!imageFolder.exists())
        {
            if (imageFolder.mkdirs())
            {
                Log.e(TAG, "createImageFile: directory was created successfully.");
            }
            else
            {
                Log.e(TAG, "createImageFile: directory was not created.");
            }
        }
        else
        {
            Log.e(TAG, "createImageFile: directory already exists.");
        }

        Log.e(TAG, "run: image folder path is: " + imageFolder.getAbsolutePath());

        File image = null;
        File mediaFile = new File(imageFolder + File.separator );
        String imageFileName = "imageName_" + System.currentTimeMillis();
        Log.e(TAG, "createImageFile: image file name is: " + imageFileName);

        try
        {
            image = File.createTempFile(
                    imageFileName,   /* prefix */
                   ".jpg",    /* suffix */
                    mediaFile       /* directory */);
        }
        catch (IOException e)
        {
            Log.e(TAG, "createImageFile: exception occurred while creating image file:\n");
            e.printStackTrace();
        }

        if (image != null)
        {
            // Save a file: path for use with ACTION_VIEW intents
            mCurrentPhotoPath = image.getAbsolutePath();
            return image;
        }
        else
        {
            Log.e(TAG, "createImageFile: image was not created.");
            return null;
        }
    }

在清单文件中添加以下内容:

Add this in your manifest file:

<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"/>
        </provider>

然后创建和xml资源目录,并添加和file_paths xml文件,并在其中添加以下内容:

Then create and xml resource directory and add and file_paths xml file and inside that add this:

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="my_images" path="PathOfYourFolder" />
</paths>

在活动结果方法中,只需使用mCurrentPhotoPath将图像加载到图像视图中即可。用毕加索来做。

And in your on activity result method just use the mCurrentPhotoPath to load the image into your image view. Use Picasso to do that.

这篇关于在牛轧糖中从相机拍摄时无法加载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆