在牛轧糖

时间:2018-04-13 13:55:13

标签: android camera

我在从无效中捕获图像时调用此方法。

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

}

我在

中创建了xml文件
  

值 - > 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。

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

Image is not loading like this

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

任何帮助都将不胜感激。

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:image file name是: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:image file name是:imageName_1523630149792

1 个答案:

答案 0 :(得分:1)

试试这个,它对我有用:

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

在清单文件中添加:

<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资源目录以及add和file_paths xml文件并在其中添加:

<?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>

在您的on活动结果方法中,只需使用mCurrentPhotoPath将图像加载到图像视图中。使用毕加索来做到这一点。