如果目录/文件夹已经存在(Android),如何通知用户?

时间:2017-03-13 14:29:56

标签: java android

我修改了一个简单的相机应用程序。基本上它会要求用户在开头输入他们喜欢的“专辑名称”。现在我正在坚持如何验证是否存在同名文件夹。我要做的是限制用户创建具有相同文件夹/目录名称的文件夹,我希望有人可以帮助我。再次感谢,

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private static String folderValue;
    private Camera mCamera;

    public static final String TAG = "CamActivity";
    private boolean isFilterOpen = false;
    private ImageView filterButton;
    private boolean isSaving = false; //onClick, onTakePicture


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Dialog Box Part
        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);

        builder.setTitle("Welcome to BloCC!");
        builder.setMessage("Please Enter Session Name");
        final EditText userInput = new EditText(MainActivity.this);
        builder.setView(userInput);
        builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                folderValue = userInput.getText().toString();

            }
        })
                .setCancelable(false)
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent(MainActivity.this, MainMenu.class);
                        startActivity(intent);
                    }
                });
        builder.show();


        // Create an instance of Camera
        mCamera = getCameraInstance(); //checks for null //titosaw2
        // Create our Preview view and set it as the content of our activity.
        CameraPreview mPreview = new CameraPreview(this, mCamera); //check for conflicts onResume
        FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);

        if (preview != null) {
            preview.addView(mPreview);
        }

        Camera.Parameters parameters = mCamera.getParameters();
        parameters.setColorEffect(Camera.Parameters.EFFECT_MONO);
        mCamera.setParameters(parameters);

        ImageView captureButton = (ImageView) findViewById(R.id.button_capture);
        if (captureButton != null) {
            captureButton.setOnClickListener(this);
        }
    }

    /**
     * A safe way to get an instance of the Camera object.
     */
    public static Camera getCameraInstance() {
        Camera c = null;
        try {
            c = Camera.open(); // attempt to get a Camera instance
        } catch (Exception e) { //check View v
            // Camera is not available (in use or does not exist)
        }
        return c; // returns null if camera is unavailable
    }


    private Camera.PictureCallback mPicture = new Camera.PictureCallback() {

        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
            File pictureFile = getOutputMediaFile();
            if (pictureFile == null) {
                Log.d(TAG, "Error creating media file, check storage permissions: ");
                return;
            }
            MediaScannerConnection.scanFile(MainActivity.this,

                    new String[]{pictureFile.toString()}, null,
                    new MediaScannerConnection.OnScanCompletedListener() {
                        public void onScanCompleted(String path, Uri uri) {
                            Log.i("ExternalStorage", "Scanned " + path + ":");
                            Log.i("ExternalStorage", "-> uri=" + uri);
                        }
                    });

            try {
                FileOutputStream fos = new FileOutputStream(pictureFile);
                fos.write(data);
                fos.close();
            } catch (FileNotFoundException e) {
                Log.d(TAG, "File not found: " + e.getMessage());
            } catch (IOException e) {
                Log.d(TAG, "Error accessing file: " + e.getMessage());
            }
            mCamera.startPreview();
            Toast.makeText(getApplicationContext(), "Photo Taken", Toast.LENGTH_SHORT).show();
            isSaving = false;
        }
    };


    private static File getOutputMediaFile() {
        //new folder or check for existing
        File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), folderValue);
        // Create the storage directory if it does not exist
        if (!mediaStorageDir.exists()) {
            if (!mediaStorageDir.mkdirs()) {
                Log.d(TAG, "failed to create directory");
                return null;
            }
        }
        // Create a media file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        return new File(mediaStorageDir.getAbsolutePath() + File.separator +
                "IMG_" + timeStamp + ".jpg");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button_capture:
                if (isSaving == false) {
                    mCamera.takePicture(null, null, mPicture); //onPictureTaken
                    isSaving = true;
                } else {
                    //stillsaving
                }
                break; //check for instance
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        //releaseMediaRecorder(); // if you are using MediaRecorder, release it first
        releaseCamera(); // release the camera immediately on pause event
    }

    private void releaseCamera() {
        if (mCamera != null) {
            mCamera.release();        // release the camera for other applications
            mCamera = null;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mCamera == null) {
            setContentView(R.layout.activity_main);

            mCamera = getCameraInstance();
            CameraPreview mPreview = new CameraPreview(this, mCamera); //check for conflicts onResume
            FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview); //check
            preview.addView(mPreview); //check for null

            Camera.Parameters parameters = mCamera.getParameters();
            parameters.setColorEffect(Camera.Parameters.EFFECT_MONO);
            mCamera.setParameters(parameters);
            //copied from onCreate
            ImageView captureButton = (ImageView) findViewById(R.id.button_capture);
            if (captureButton != null) {
                captureButton.setOnClickListener(this);
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        android.os.Process.killProcess(android.os.Process.myPid()); //test see manifest //fixed

    }
}

0 个答案:

没有答案