Ask God to help solve the problem of android mobile phone taking function. Using provider will cause APP to flash back and go in when it opens. One
In Manifest, user rights corresponding to provider and camera are added:
<application <provider android:name="android.support.v4.content.FileProvider" android:authorities="com.example.myapplication.fileprovider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths"/> </provider> </application> <uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
In onCreate of MainActivity, listen for the button (mTakePhoto) to enter the photo function to be pressed
mTakePhoto.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // // Following is the photo code for the first use of providers. Adding providers in Manifest will cause APP to flicker, commenting out providers can enter APP, but clicking on the Photo button will also flicker. // // Create a file object to store the photographed image. File outputImage = new File(getExternalCacheDir(), "output_image.jpg"); try { if (outputImage.exists()) { outputImage.delete(); } outputImage.createNewFile(); } catch (Exception e) { e.printStackTrace(); } if (Build.VERSION.SDK_INT >= 24) { //Compatible with Android 7.0 in the form of shared files imageUri = FileProvider.getUriForFile(MainActivity.this, "com.example.myapplication.fileprovider", outputImage); } else { imageUri = Uri.fromFile(outputImage); } //Start Camera Program Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri ); startActivityForResult(intent, TAKE_PHOTO); } });
The file_path.xml file is created in the following directory. The contents of the file are shown in the code below.
<?xml version = "1.0" encoding = "utf-8"?> <paths xmlns:android = "http://schemas.android.com/apk/res/android"> <external-path name = "my_images" path = "/sdcard/DCIM/camera"></external-path> </paths>
App's interface is as follows, ignoring other functions, just look at take_photo, the normal logic is to click the button to enter the camera function, but if the above-mentioned add provider, even APP can not open at all. This page can be seen only after annotating the code of provider in manifest, but clicking take_photo will flicker back. I don't know what's wrong, but also ask God to bestow it on you. Teaching. This is my first time to develop android. I haven't touched it before, and the code is also made according to the online method. Please explain in detail when the gods explain it. Thank you.