1

我按照 Youtube 的教程构建了一个相机应用程序。它使用以下代码将文件保存在外部存储中

public void takePicture(View view) {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    file = Uri.fromFile(getOutputMediaFile());
    intent.putExtra(MediaStore.EXTRA_OUTPUT, file);

    startActivityForResult(intent, 100);
}

private static File getOutputMediaFile() {
    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES), "CameraDemo");

    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            Log.d("CameraDemo", "failed to create directory");
            return null;
        }
    }

    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    return new File(mediaStorageDir.getPath() + File.separator +
            "IMG_" + timeStamp + ".jpg");
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 100) {
        if (resultCode == RESULT_OK) {
            imageView.setImageURI(file);
        }
    }
}

但是,当应用程序从目录选择器对话框开始时,我想向用户显示一个对话框,他们想要保存图像,我按照此链接https://www.codeproject.com/Articles/547636/Android-Ready中的教程进行操作-to-use-simple-directory-chooser-dial

目录选择对话框的代码如下

DirectoryChooserDialog directoryChooserDialog =
    new DirectoryChooserDialog(MainActivity.this,
        new DirectoryChooserDialog.ChosenDirectoryListener()
        {
            @Override
            public void onChosenDir(String chosenDir)
            {
                m_chosenDir = chosenDir;
                Toast.makeText(
                    MainActivity.this, "Chosen directory: " +
                    chosenDir, Toast.LENGTH_LONG).show();
            }
        });
// Toggle new folder button enabling
directoryChooserDialog.setNewFolderEnabled(m_newFolderEnabled);
// Load directory chooser dialog for initial 'm_chosenDir' directory.
// The registered callback will be called upon final directory selection.
directoryChooserDialog.chooseDirectory(m_chosenDir);
m_newFolderEnabled = ! m_newFolderEnabled;

而且确实,我有一个单独的 DirectoryChooserDialog 类。

现在我如何将第一个代码与第二个代码合并,这样当从相机拍摄的图像将保存到用户从第二个代码中选择的文件夹中,而不是第一个代码指定的外部存储目录中。

DirectoryChooserDialog 的图像文件显示为

目录选择器对话框

4

0 回答 0