Мне нужно открыть камеру и сделать снимок и отобразить его в режиме просмотра изображений.
Но проблема в том, что я получаю сбой, как показано ниже.Я почистил сборку и побежал.Но все же эта проблема возникает.
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)
Вот код.Я не знаю, какую ошибку я делаю.Пожалуйста, поправьте меня.
Я даже добавил разрешения в файле манифеста.
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 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:
<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>