我需要打开相机并拍照并在imageview中显示。
但问题是我正在崩溃,如下所示。我清理了构建并运行。但仍然会出现这个问题。
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>
答案 0 :(得分:0)
public void openImageChooser() {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
点击按钮打开相机..
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);
}
在onActivityResult中获取位图并在ImageView中设置它
event.stopPropagation()
答案 1 :(得分:0)
使用CameraIntent打开相机:
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, 200);
在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);
}
}
}