Android 文件提供程序从照相机拍摄照片时出现问题

Android 文件提供程序从照相机拍摄照片时出现问题,android,camera,photo,androidx,fileprovider,Android,Camera,Photo,Androidx,Fileprovider,我正在尝试制作一个程序,从相机上拍摄高质量的照片,并显示在imageview中/因此,如果我尝试在Android 9和Android 7上运行代码,其工作正常/但当我尝试将其放入Android 4.1.1 API 16时,其不工作相机在拍照后单击“确定”时停止,因此我认为拍照代码正常,但问题进入(publicsvoidonactivityresult),所以请帮助我 public class MainActivity extends AppCompatActivity { Intent int

我正在尝试制作一个程序,从相机上拍摄高质量的照片,并显示在imageview中/因此,如果我尝试在Android 9和Android 7上运行代码,其工作正常/但当我尝试将其放入Android 4.1.1 API 16时,其不工作相机在拍照后单击“确定”时停止,因此我认为拍照代码正常,但问题进入(publicsvoidonactivityresult),所以请帮助我

public class MainActivity extends AppCompatActivity {

Intent intent;
    Uri fileUri, imageUri;
    Button btn_choose_image;
    ImageView imageView;
    Bitmap bitmap, decoded;
    public final int REQUEST_CAMERA = 1;
    public final int SELECT_FILE = 1;
    String currentImagePath = null;
    private static final int IMAGE_REQUEST = 1;
    int bitmap_size = 40; // image quality 1 - 100;
    int max_resolution_image = 800;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn_choose_image = (Button) findViewById(R.id.btn_choose_image);
        imageView = (ImageView) findViewById(R.id.image_view);

        btn_choose_image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectImage();
            }
        });
        // StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
        //  StrictMode.setVmPolicy(builder.build());
    }

    private void selectImage() {
        imageView.setImageResource(0);
        final CharSequence[] items = {"Take Photo", "Choose from Library",
                "Cancel"};

        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setTitle("Add Photo!");
        builder.setIcon(R.mipmap.ic_launcher_round);
        builder.setItems(items, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int item) {
                if (items[item].equals("Take Photo")) {

                    Intent Inatent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    if (Inatent.resolveActivity(getPackageManager()) != null) {
                        File imageFile = null;
                        try {
                            imageFile = getImageFile();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        if (imageFile != null) {
                            imageUri = FileProvider.getUriForFile(MainActivity.this, "com.example.fileprovider", imageFile);
                            Inatent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
                            startActivityForResult(Inatent, REQUEST_CAMERA);
                        }
                    }
                } else if (items[item].equals("Choose from Library")) {
                    intent = new Intent();
                    intent.setType("image/*");
                    intent.setAction(Intent.ACTION_GET_CONTENT);
                    startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_FILE);
                } else if (items[item].equals("Cancel")) {
                    dialog.dismiss();
                }
            }
        });
        builder.show();
    }



  @Override
    public void onActivityResult(int requestCode,int resultCode,Intent data){
        // Result code is RESULT_OK only if the user captures an Image
        if (resultCode == Activity.RESULT_OK)
            switch (requestCode){
                case REQUEST_CAMERA:
                    imageView.setImageURI(Uri.parse(currentImagePath));
                    break;
            }
    }


        private void setToImageView(Bitmap bmp) {

        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        bmp.compress(Bitmap.CompressFormat.JPEG, bitmap_size, bytes);
        decoded = BitmapFactory.decodeStream(new ByteArrayInputStream(bytes.toByteArray()));
        imageView.setImageBitmap(decoded);
    }

    // Untuk resize bitmap
    public Bitmap getResizedBitmap(Bitmap image, int maxSize) {
        int width = image.getWidth();
        int height = image.getHeight();

        float bitmapRatio = (float) width / (float) height;
        if (bitmapRatio > 1) {
            width = maxSize;
            height = (int) (width / bitmapRatio);
        } else {
            height = maxSize;
            width = (int) (height * bitmapRatio);
        }
        return Bitmap.createScaledBitmap(image, width, height, true);
    }





    private File getImageFile() throws IOException {

        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageName="jpg_"+timeStamp+"_";
        File storagDir =getExternalFilesDir(Environment.DIRECTORY_PICTURES);
        File imaeFile=File.createTempFile(imageName,".jpg",storagDir);
        currentImagePath=imaeFile.getAbsolutePath();
        return imaeFile;
    }
}


张贴日志中的行,告诉你为什么你的应用程序会崩溃。使用前检查currentImagePath==null。注意Android 4.1.1非常旧。旧版本Android上的一些摄像头应用程序可能不希望在
额外输出中出现
内容
Uri
。不幸的是,您无法事先知道用户首选的相机应用程序是否需要
文件
Uri
,内容
,或者干脆完全忽略额外的输出。logact 2020-01-19 20:29:11.375 1715-1715/com.example.myapplication E/zyote:isWhitelistProcess-进程被列入白名单2020-01-19 20:29:11.376 1715-1715/com.example.myapplication E/zyote:accessInfo:1@Marwan请在问题本身中包含相关的logcat调试/错误日志(格式化为代码)发布logcat中的行,告诉你为什么你的应用程序会崩溃。使用前检查currentImagePath==null。注意Android 4.1.1非常旧。旧版本Android上的一些摄像头应用程序可能不希望在
额外输出中出现
内容
Uri
。不幸的是,您无法事先知道用户首选的相机应用程序是否需要
文件
Uri
,内容
,或者干脆完全忽略额外的输出。logact 2020-01-19 20:29:11.375 1715-1715/com.example.myapplication E/zyote:isWhitelistProcess-进程被列入白名单2020-01-19 20:29:11.376 1715-1715/com.example.myapplication E/zyote:accessInfo:1@Marwan请在问题本身中包含相关的logcat调试/错误日志(格式化为代码)
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="external_files" path="."/>
</paths>
<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-feature android:name="android.hardware.camera" />

   <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="com.example.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths" />
        </provider>

01-19 18:17:41.379 1930-1936/? E/jdwp: Failed writing handshake bytes: Broken pipe (-1 of 14)

01-19 18:17:41.723 1930-1930/? E/dalvikvm: Could not find class 'androidx.core.view.ViewCompat$OnUnhandledKeyEventListenerWrapper', referenced from method androidx.core.view.ViewCompat.addOnUnhandledKeyEventListener


01-19 18:17:41.727 1930-1930/? E/dalvikvm: Could not find class 'android.view.WindowInsets', referenced from method androidx.core.view.ViewCompat.dispatchApplyWindowInsets


01-19 18:17:41.775 1930-1930/? E/dalvikvm: Could not find class 'android.view.WindowInsets', referenced from method androidx.core.view.ViewCompat.onApplyWindowInsets

01-19 18:17:41.779 1930-1930/? E/dalvikvm: Could not find class 'android.view.View$OnUnhandledKeyEventListener', referenced from method androidx.core.view.ViewCompat.removeOnUnhandledKeyEventListener


01-19 18:17:41.811 1930-1930/? E/dalvikvm: Could not find class 'androidx.core.view.ViewCompat$1', referenced from method androidx.core.view.ViewCompat.setOnApplyWindowInsetsListener


01-19 18:17:41.891 1930-1930/? E/dalvikvm: Could not find class 'android.graphics.drawable.RippleDrawable', referenced from method androidx.appcompat.widget.AppCompatImageHelper.hasOverlappingRendering