【问题标题】:Create folder in Android in DCIM folder (Sorry, another one.)在 Android 中的 DCIM 文件夹中创建文件夹(对不起,另一个。)
【发布时间】:2019-10-16 22:05:27
【问题描述】:

好的,我正在 Unity 中制作游戏,我正在尝试截取屏幕截图并将其保存到我在 DCIM 文件夹中创建的文件夹中。我尝试在 Unity 中创建文件夹,但从未成功。 我尝试在 Android Studio 中创建一个插件来做到这一点,也没有运气。

这是我的 Java 代码:

package foldercreator.TehBestCompany.com.foldermakerlibrary;

import android.os.Environment;
import android.util.Log;

import java.io.File;
import java.lang.String;


public class FolderMaker {

    public boolean MoveFile( String filename, String sourceFolder, String destinationFolder )
    {
        String fullDestinationFolder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).toString() + "/" + destinationFolder;

        Log( "Destination folder " + fullDestinationFolder);

        final File f = new File( fullDestinationFolder );

        if (!f.exists()) {
            Log( "Folder doesn't exist, creating it...");
            boolean rv = f.mkdir();
            Log( "Folder creation " + ( rv ? "success" : "failed"));
        } else {
            Log( "Folder already exists.");
        }

        return true;
    }



    private void Log( String message )
    {
        Log.d("[LOG FILE]", message );
        System.out.println( "[LOG FILE] " + message );
    }
}

当我运行代码时,我总是收到“文件夹创建失败”消息。

这是我的库清单(如果有帮助,我已经检查了 Unity 中的使用外部存储选项):

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="foldercreator.TehBestCompany.com.foldermakerlibrary" >
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>

谁能给我一些建议?

我知道这个问题以前被问过很多次,但是在尝试了各种解决方案一整天之后,我完全没有想法。

【问题讨论】:

    标签: java android unity3d


    【解决方案1】:

    对我来说,当我更改此行时它起作用了:

    String fullDestinationFolder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).toString() + "/" + destinationFolder;
    

    收件人:

    String fullDestinationFolder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)+"/destenationFolder");
    

    然后:

    File fileName = new File(fullDesinationFolder, "screenshot.jpeg");
    

    或者直接做:

    File fileName = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)+"/destenationFolder"), "screenshot.jpeg");
    

    【讨论】:

    • 谢谢你的回答,但是我不能做你的第一个建议(IE)...DIRECTORY_DCIM)+"/destenationFolder"); 因为destinationFolder 是通过统一传递到这个插件中的,所以必须像我在我的代码中那样构建.至于你的第二个建议,你建议创建一个文件,然后检查它是否存在,但它永远不会存在,因为我还没有把它移到那里。我正在尝试创建将文件移动到的目标文件夹。
    • 不加.toString()也不行? (在DIRECTORY_DCIM之后)
    • 没有。我这样做了,但仍然收到文件夹创建失败消息。 String fullDestinationFolder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) + "/" + destinationFolder; final File f = new File( fullDestinationFolder, filename ); if (!f.exists()) { Log( "Folder doesn't exist, creating it..."); boolean rv = f.mkdirs(); Log( "Folder creation " + ( rv ? "success" : "failed")); } else { Log( "Folder already exists."); }
    • 我看到你在DIRECTORY_DCIM之后关闭了括号...在destinationFolder之后才关闭括号...Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM + "/" + destinationFolder)
    • 试过了,还是不行。最终文件 f = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM + "/" + destinationFolder), 文件名); if (!f.exists()) { Log("文件夹不存在,正在创建...");布尔 rv = f.mkdirs(); Log("文件夹创建" + ( rv ? "success" : "failed")); if( !rv ) 返回假; } else { Log("文件夹已经存在"); }
    猜你喜欢
    • 1970-01-01
    • 2019-02-21
    • 1970-01-01
    • 2011-07-04
    • 1970-01-01
    • 2017-01-27
    • 1970-01-01
    • 1970-01-01
    • 2022-01-11
    相关资源
    最近更新 更多