java - 如何修复相机 API?

标签 java android camera-api

嗨,我正在尝试打开相机,我看到了有关如何使用它的 android 文档。

所以我只是将其复制粘贴到我的代码中,并且我已经设置了它的权限,但是当我单击按钮时,相机没有打开。

  @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {


        if(requestCode == CAMERA_REQUEST_CODE){
            Log.d("ano_code",Integer.toString(requestCode));
          if(resultCode == Activity.RESULT_OK){

              File file = new File(currentPhotoPath);
              selectedImage.setImageURI(Uri.fromFile(file));

          }else{
              Log.d("mali","mali nga");
          }


        }

        if(requestCode == GALLERY_REQUEST_CODE){
            if(resultCode == Activity.RESULT_OK){
                Uri contentUri = data.getData();
                String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
                String imageFileName = "JPEG_" + timeStamp +"."+getFileExt(contentUri);
                Log.d("tag", "onActivityResult: Gallery Image Uri:  " +  imageFileName);
                selectedImage.setImageURI(contentUri);


            }

        }


    }



    private String getFileExt(Uri contentUri) {
        ContentResolver c = getContentResolver();
        MimeTypeMap mime = MimeTypeMap.getSingleton();
        return mime.getExtensionFromMimeType(c.getType(contentUri));
    }
    private File createImageFile() throws IOException {
        // Create an image file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "JPEG_" + timeStamp + "_";
        //File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
        File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
        File image = File.createTempFile(
                imageFileName,  /* prefix */
                ".jpg",         /* suffix */
                storageDir      /* directory */
        );

        // Save a file: path for use with ACTION_VIEW intents
        currentPhotoPath = image.getAbsolutePath();
        return image;
    }
   private void dispatchTakePictureIntent() {
        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) {

            }
            // Continue only if the File was successfully created
            if (photoFile != null) {
                Log.d("qqqqq","dito na ao4");
                Uri photoURI = FileProvider.getUriForFile(this,
                        "com.example.test.android.fileprovider",
                        photoFile);
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                startActivityForResult(takePictureIntent, CAMERA_REQUEST_CODE);
            }
        }
    }

Manifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.test">

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.CAMERA"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-feature android:name="android.hardware.camera" android:required="true"/>


    <!--This should have the Activity files ONLY-->

    <application
        android:hardwareAccelerated="false"
        android:largeHeap="true"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme.NoActionBar">
        <activity
            android:name=".Utilities.SplashScreen"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="com.example.test.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>
 </application>

</manifest>

我真的不知道该怎么办,没有错误,有人也遇到过这种情况吗?

这是文档的链接。

https://developer.android.com/training/camera/photobasics

最佳答案

你可以在 list 中声明提供商吗?是,然后粘贴来自 logcat 的错误消息。

关于java - 如何修复相机 API?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60207413/

相关文章:

java - 大型 TicTacToe 5x5 棋盘的非常好的启发式评估规则

c# - C# 中的 Guice 等价物

Android 动画留下伪影

android - java.lang.RuntimeException : takePicture failed in lollipop and marshmallow 错误

java - 如何基于 2 个键创建 JDBC upsert 查询

java - 从服务更新 textView 的最简单方法是什么?

android - 将 JSONArray 转换为字符串数组

android - 将 netbeans android 项目导入 android studio

android - Android webview 中的 HTML 相机 API