如何动态更改Android Studio的文件夹名称文件路径?

问题描述 投票:1回答:1

我需要你的帮助。我开始创建一个应用程序,我将保存照片,视频,人声备忘录等,如帖子/便笺,所有内容均属于此帖子。

这些多媒体文件将保存在一个名为时间戳的文件夹中。通过这种方式,每个帖子/注释都由时间戳表征。每当我要创建新帖子时,时间戳应该更新。

我想通过以下路径保存文件:

  • Name_app
    • 时间戳
      • 图片
        • Pictures1
        • Pictures2
        • ...
      • 视频
        • Video1
        • ...

在我的代码下面。

这是创建我的图像文件的代码(看起来很正常):

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;
}

这是开始活动的代码:

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);
        }
    }
}

我认为问题出在“ Uri photoURI = FileProvider.getUriForFile(this,“ com.example.andreacarubelli.urbanstoriessharing.fileprovider”,photoFile);“

这是我的路径代码(而且我不知道如何像这样动态更改它->“ 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>

这是我在Android清单中的提供者:

<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>

非常感谢。

android xml gradle
1个回答
0
投票

旧,但我有同样的问题...

© www.soinside.com 2019 - 2024. All rights reserved.