将文件保存到文件系统

时间:2019-02-24 07:53:22

标签: java android file

我已经尝试解决这个问题了好几个小时,但是我却无所适从。我目前正在使用Android Studio,并尝试保存使用Intent(MediaStore.ACTION_IMAGE_CAPTURE)拍摄的照片。但是,我遇到了许多导致应用程序崩溃的错误。

主要活动代码

public void openCamera(View view) {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    imageFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "test.jpg");
    Uri tempUri = Uri.fromFile(imageFile);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, tempUri);
    intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
    startActivityForResult(intent,1);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(requestCode == 0) {
        switch (resultCode) {
            case Activity.RESULT_OK:
                if(imageFile.exists()) {
                    Toast.makeText(this, "The File was saved at " + imageFile.getAbsolutePath(), Toast.LENGTH_LONG).show();
                }
                else {
                    Toast.makeText(this, "There was an error saving the file", Toast.LENGTH_LONG).show();
                }
                break;
            case Activity.RESULT_CANCELED:

                break;

            default:
                break;
        }
    }
}

0 个答案:

没有答案