简体   繁体   English

尝试打开相机android时崩溃?

[英]Crash when try to open camera android?

I need to open camera and take picture and display in imageview. 我需要打开相机并拍照并在imageview中显示。

But problem is I am getting crash as below. 但是问题是我正在崩溃,如下所示。 I cleaned the build and ran. 我清理了构建并运行。 But still this problem occurs. 但是仍然会出现此问题。

 Caused by: java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/Android/data/com.trial.learn/files/Pictures/JPEG_20180515_104113_6007506636120272936.jpg
                                                                     at android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:711)
                                                                     at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:400)
                                                                     at com.trial.learn.MainActivity.openCamera(MainActivity.java:57)
                                                                     at com.trial.learn.MainActivity.checkCameraPermission(MainActivity.java:37)
                                                                     at com.trial.learn.MainActivity.onCreate(MainActivity.java:31)
                                                                     at android.app.Activity.performCreate(Activity.java:6998)
                                                                     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1230)
                                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2899)
                                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3046) 
                                                                     at android.app.ActivityThread.-wrap11(Unknown Source:0) 
                                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1688) 
                                                                     at android.os.Handler.dispatchMessage(Handler.java:105) 
                                                                     at android.os.Looper.loop(Looper.java:164) 
                                                                     at android.app.ActivityThread.main(ActivityThread.java:6809) 
                                                                     at java.lang.reflect.Method.invoke(Native Method) 
                                                                     at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

Here is code. 这是代码。 I am clueless what mistake I am doing. 我不知道自己在做什么错。 Kindly correct me. 请纠正我。

I even added permissions in manifest file too. 我什至在清单文件中也添加了权限。

public class MainActivity extends AppCompatActivity {
    private String mCurrentPhotoPath,mCurrentPhotoPathResize;
    private ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView = (ImageView)findViewById(R.id.imageView);
        checkCameraPermission();
    }
    private void checkCameraPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
            if (!checkPermission(this, Manifest.permission.CAMERA, 123))
                checkPermission(this, Manifest.permission.CAMERA, 123);
            else openCamera();
        else openCamera();
    }
    private void openCamera() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        if (takePictureIntent.resolveActivity(this.getPackageManager()) != null) {
            File photoFile = null;
            File photoFile2 = null;
            try {
                photoFile = ImageResizeUtility.createImageFile(this);
                photoFile2 = ImageResizeUtility.createImageFile2(this);
            } catch (IOException ex) {
                // Error occurred while creating the File
            }
            mCurrentPhotoPath = photoFile.getAbsolutePath();
            mCurrentPhotoPathResize = photoFile2.getAbsolutePath();
            Log.e("url",mCurrentPhotoPath);
            Log.e("urls",mCurrentPhotoPathResize);

            Uri photoURI = FileProvider.getUriForFile(MainActivity.this, "com.trial.learn.authority", photoFile);
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
            startActivityForResult(takePictureIntent, 111);
        }
    }

    public static boolean checkPermission(Activity context, String permission, int requestCode) {
        if (ContextCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(context,
                    new String[]{permission},
                    requestCode);
            return false;
        } else {
            return true;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 111 && resultCode == RESULT_OK) {
            ImageResizeUtility.compressImage(this, mCurrentPhotoPath, mCurrentPhotoPathResize, imageView);
            String _imagePath = mCurrentPhotoPathResize;
            Log.e("urlOnActivity",_imagePath);
        }
    }
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case 123:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
                    openCamera();
                break;
        }
    }
}

xml/file_paths.xml: xml / 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="Android/data/com.trial.learn/files/Pictures" />
</paths>

android manifest.xml: android manifest.xml:

<uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />


<provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="com.trial.learn.authority"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
        </provider>
private int CAMERA_REQUEST = 2000;

Open Camera on button click.. 单击按钮打开相机。

 public void openImageChooser() {
    Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(cameraIntent, CAMERA_REQUEST);
}

And in onActivityResult fetch the bitmap and set it in ImageView 然后在onActivityResult中获取位图并在ImageView中进行设置

  public void onActivityResult(int requestCode, int resultCode, Intent data) 
  {

   if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
        Bitmap photo = (Bitmap) data.getExtras().get("data");

        ((ImageView) findViewById(R.id.frontimg)).setImageBitmap(photo);
  }

Open Camera with CameraIntent: 使用CameraIntent打开相机:

Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(cameraIntent, 200);

Get Captured Image in onActivityResult: 在onActivityResult中获取捕获的图像:

public void onActivityResult(int requestCode, int resultCode, Intent data) {

    switch (requestCode) {

        case 200:

            if (resultCode == Activity.RESULT_OK) {

                if (data != null) {

                    Bitmap cameraImage = (Bitmap) data.getExtras().get("data");


                    imageView.setImageBitmap(cameraImage);

             }
            }
         }

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

相关问题 Android相机无法对焦。 当我尝试设置参数时崩溃 - Android camera not focusing. Crash when I try to setParameters 当我尝试打开活动时,android 应用程序崩溃 - android application crash when i try to open activity Android(Java)-Camera.Open()崩溃; - Android (Java) - Crash by Camera.Open(); Android-每次我尝试开启应用程式时都会当机 - Android - App Crash everytime I try to open it 当我尝试打开活动时应用程序崩溃 - Application crash when I try to open the activity android.view.InflateException,尝试打开菜单时Android应用程序崩溃! - android.view.InflateException, Android application crash when I try to open menu! 为什么在使用android.hardware.Camera.Open()时我的应用程序崩溃? - Why does my app crash when using android.hardware.Camera.Open()? Android:当我在应用程序中打开相机模型时,需要一个APK(处于发布模式)。 应用会崩溃吗? - Android: After relese an APK (in release mode), when i open camera model in an application. App will crash? 尝试打开浏览器(或任何其他隐式意图)意图时,Android崩溃 - Android crash when try to open browser(or any other implicit intent) intent 应用程序崩溃时使用CAMERA - Android - Application Crash when using CAMERA - Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM