How to change dynamically folder name file paths Android Studio?

Viewed 569

I need your help. I'm starting to create an app, and I would save photos, videos, vocal memo etc. like a post/note and everything are part of this post.

These multimedia files, will be saved in a folder, which name is a timestamp. On this way, every post/note is charaterized by the timestamp. The timestamp should update every time when I want to create a new post.

I want to save my files by following this path:

  • Name_app
    • Timestamp
      • Pictures
        • Pictures1
        • Pictures2
        • ...
      • Videos
        • Video1
        • ...

Below my code.

This is the code which create my Image File (and it look like to work):

private File createImageFile () throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File root = new File(Environment.getExternalStorageDirectory().toString());
    File myDir = new File(root + "/Urban_stories_sharing/" + timeStamp + "/Pictures");
    if (!myDir.exists()) {
        myDir.mkdirs();
    }
    File image = File.createTempFile(
            imageFileName,  /* prefix */
            ".jpg",         /* suffix */
            myDir      /* directory */
    );

    // Save a file: path for use with ACTION_VIEW intents
    mCurrentPhotoPath = image.getAbsolutePath();
    return image;
}

This is the code which start the activity:

public void goToCamera(View v) {
    getCameraPermission();
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        File photoFile = null;
        try {
            isStoragePermissionGranted();
            photoFile = createImageFile();
        } catch (IOException ex) {
            // Error occurred while creating the File
        }
        // Continue only if the File was successfully created
        if (photoFile != null) {
            Uri photoURI = FileProvider.getUriForFile(this,
                    "com.example.andreacarubelli.urbanstoriessharing.fileprovider",
                    photoFile);
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
            startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
        }
    }
}

I think that the problem is on "Uri photoURI = FileProvider.getUriForFile(this, "com.example.andreacarubelli.urbanstoriessharing.fileprovider", photoFile);"

This is the code of my path (and I don't know how to change it dynamically like this -> "Urban_stories_sharing/" + timeStamp + "/Pictures"):

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="my_images"
    path="Urban_stories_sharing/Pictures" />
</paths>

This is my provider in my Android Manifest:

<provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="com.example.andreacarubelli.urbanstoriessharing.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths">

        </meta-data>
    </provider>

Thank you so much.

0 Answers
Related