无法打开相机

时间:2019-11-06 14:02:26

标签: android camera android-camera android-camera-intent

我正在尝试打开相机,拍摄照片并将其保存在文件夹中。但是,在按下按钮时,应用程序将关闭。

public class MainActivity extends AppCompatActivity{
    private Button camera;
    private static int SAVE_IMAGE= 101;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        camera= (Button) findViewById(R.id.btnTakePicture);
        camera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent openCamera= new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                File imageFolder= new File(Environment.getExternalStorageDirectory(),"Images");
                imageFolder.mkdirs();
                String imageName= getImageName();
                File imageFile= new File(imageFolder, imageName);
                Uri imageUri= Uri.fromFile(imageFile);
                openCamera.putExtra(MediaStore.EXTRA_OUTPUT, imageFile);
                startActivityForResult(openCamera, SAVE_IMAGE);
            }
        });
    }

    private String getImageName(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
        String timestamp = sdf.format(new Date());
        return "Saved" + timestamp + ".jpg";
    }
}

错误:

Process: com.example.cmara, PID: 5460
    android.os.FileUriExposedException: file:///storage/emulated/0/Images/Saved20191106_105812.jpg exposed beyond app through ClipData.Item.getUri()
        at android.os.StrictMode.onFileUriExposed(StrictMode.java:1958)
        at android.net.Uri.checkFileUriExposed(Uri.java:2356)
        at android.content.ClipData.prepareToLeaveProcess(ClipData.java:944)
        at android.content.Intent.prepareToLeaveProcess(Intent.java:10494)
        at android.content.Intent.prepareToLeaveProcess(Intent.java:10479)
        at android.app.Instrumentation.execStartActivity(Instrumentation.java:1617)
        at android.app.Activity.startActivityForResult(Activity.java:4564)
        at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:767)
        at android.app.Activity.startActivityForResult(Activity.java:4522)
        at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:754)
        at com.example.cmara.MainActivity$1.onClick(MainActivity.java:52)
        at android.view.View.performClick(View.java:6897)
        at android.widget.TextView.performClick(TextView.java:12693)
        at android.view.View$PerformClick.run(View.java:26100)
        at android.os.Handler.handleCallback(Handler.java:789)
        at android.os.Handler.dispatchMessage(Handler.java:98)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6942)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

帮助,我不明白这是什么错误 如何将图像保存在文件夹中? 如何正确打开相机?

0 个答案:

没有答案